Relationship in MongoDB represents how the Documents are related. Relationship can be modeled through the Embeded and Referenced method . These Relationship can be 1: 1, 1: N, N: 1, or N: N.

Let us consider the case of storing the user's address. A user can have multiple addresses, which creates a 1: N Relationship.

Here is the hypothetical structure for the user document:

 { "_id" : ObjectId ( "52ffc33cd85242f436000001" ), "name" : "Tom Hanks" , "contact" : "987654321" , "dob" : "01-01-1991" } 

This is the hypothetical structure for address document:

 { "_id" : ObjectId ( "52ffc4a5d85242602e000000" ), "building" : "22 A, Indiana Apt" , "pincode" : 123456 , "city" : "Los Angeles" , "state" : "California" } 

Modeling Embeded Relationships

In the Embeded method, we embed the address document into the user document.

 { "_id" : ObjectId ( "52ffc33cd85242f436000001" ), "contact" : "987654321" , "dob" : "01-01-1991" , "name" : "Tom Benzamin" , "address" : [ { "building" : "22 A, Indiana Apt" , "pincode" : 123456 , "city" : "Los Angeles" , "state" : "California" }, { "building" : "170 A, Acropolis Apt" , "pincode" : 456789 , "city" : "Chicago" , "state" : "Illinois" }] } 

This method maintains all relevant data in a single Document, which makes data retrieval and maintenance easy. The entire Document can be retrieved in a single query, like:

 > db . users . findOne ({ "name" : "Tom Benzamin" },{ "address" : 1 }) 

Note that, in the above query, db and users are Database and Collection respectively.

The downside is that if the embedded Document continues to increase in size too much, it will affect read / write performance.

Modeling Referenced Relationship

This is a standardized Relationship design method. In this method, both the user and the address document will remain separately, but the user document will contain a field that will refer to the id field of the address document.

 { "_id" : ObjectId ( "52ffc33cd85242f436000001" ), "contact" : "987654321" , "dob" : "01-01-1991" , "name" : "Tom Benzamin" , "address_ids" : [ ObjectId ( "52ffc4a5d85242602e000000" ), ObjectId ( "52ffc4a5d85242602e000001" ) ] } 

As above, the user document contains the address_ids field, which contains ObjectIds of the corresponding address. Using these ObjectIds, we can query the address document and get the address details from there. With this approach, we will need two queries: first get the address_ids fields from the user document and then get these addresses from the address collection .

 > var result = db . users . findOne ({ "name" : "Tom Benzamin" },{ "address_ids" : 1 }) > var addresses = db . address . find ({ "_id" :{ "$in" : result [ "address_ids" ]}}) 

According to Tutorialspoint

Previous post: MongoDB Deployment

Next article: Reference Database in MongoDB

4.5 ★ | 2 Vote | 👨 317 Views

Above is an article about: "Relationship in MongoDB". Hope this article is useful to you. Don't forget to rate the article, like and share this article with your friends and relatives. Good luck!

« PREV POST
NEXT POST »