>MongoDB - Relationships

MongoDB - Create Collection: In this chapter Relationships in MongoDB.

Relationships can be both 1:1, 1:N, N:1 or N:N

Let us don't forget the case of storing addresses for users.

Following is the sample document structure of person document

   "call": "ADITYA",
   "contact": "565114",
   "dob": "01-01-1975"
is the pattern file shape of address file

   "constructing": "22 A, Indiana Apt",
   "pincode": 123456,
   "town": "LA",
   "nation": "CA"

Modeling Embedded Relationships

In the embedded approach, we will embed the cope with document inside the consumer file.

   "touch": "987654321",
   "dob": "01-01-1991",
   "call": "Tom Benzamin",
   "address": [
         "building": "22 A, Indiana Apt",
         "pincode": 123456,
         "city": "Los Angeles",
         "state": "California"
         "building": "170 A, Acropolis Apt",
         "pincode": 456789,
         "town": "Chicago",
         "kingdom": "Illinois"

Modeling Referenced Relationships

This is the method of designing normalized dating. In this method, each the user and cope with documents can be maintained one at a time however the person file will comprise a area so that it will reference the cope with report's identity discipline.

   "contact": "987654321",
   "dob": "01-01-1991",
   "name": "Tom Benzamin",
   "address_ids": [

This is the approach of designing normalized dating. In this method
>var result = db.Users.FindOne("call":"Tom Benzamin","address_ids":1)
>var addresses = db.Cope with.Discover("_id":"$in":result["address_ids"])

welookups is optimized for learning.© welookups 2018 -
All Right Reserved and you agree to have read and accepted our term and condition