Relationship in MongoDB
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
You should read it
- Reference Database in MongoDB
- Relationships between tables in Access
- How to change relationships on Facebook
- 8 signs of your relationship will not go anywhere
- Advantages of MongoDB
- Data modeling in MongoDB
- 20 free 3D modeling software
- Standardize databases in Access - Relationships between tables
- 14 questions you need to answer before starting a new relationship
- 1-n relationship between tables in Access
- How to improve relationships in just 60 seconds?
- How to Slow Down a Relationship
Maybe you are interested
How to scan documents directly using WhatsApp
Excel error Document not saved, super good fix
9 time-saving tips when formatting documents in Microsoft Word
Create images for documents using Gemini in Google Docs
How to create QR codes for PDF, Word, Excel documents for free, super fast
Turn any document into a presentation with this AI trick!