Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

Relationship in Mongodb: What You Need to Know

Understand Relationship in Mongodb with clear explanations, practical examples, and useful tips. This updated guide covers the essential concepts and common...

Table of Contents

Relationship in Mongodb is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.

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" ]}}) 

FAQ

What should you know about modeling Embeded Relationships?

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

What should you know about 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.

What is Relationship in Mongodb?

Relationship in MongoDB represents how the Documents are related.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.