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

4.5 ★ | 2 Vote

May be interested

  • Reference Database in MongoDBPhoto of Reference Database in MongoDB
    as shown in the relationship chapter in mongodb, to deploy a standardized database structure in mongodb, we use the referenced relationship concept, also known as manual references, in which we manipulate to store the id of the documents referenced in another document.
  • Covered Query in MongoDBPhoto of Covered Query in MongoDB
    when all fields in the query are part of the index, mongodb connects query conditions and returns the result by using the same index without looking inside the document. when indexes are present in ram, retrieving data from indexes is faster when compared to retrieving data by scanning all documents.
  • Query analysis in MongoDBPhoto of Query analysis in MongoDB
    analyzing queries is a very important aspect to assess the effectiveness of database and the effectiveness of the designed index. we will explore the two most frequently used queries, $ explain and $ hint.
  • Atomic Operation in MongoDBPhoto of Atomic Operation in MongoDB
    mongodb does not support atomic transaction over multiple documents. however, it provides atomic operations on a single document. therefore, if a document has hundreds of fields, the update command will either update all those fields or not update any fields, thus maintaining atomicity at the document level.
  • Advanced index operation in MongoDBPhoto of Advanced index operation in MongoDB
    create an index on the array ie create separate indexes for each of its fields. so in this situation, when we create an index on the array of tags, individual indexes will be created for their values: music, cricket and blogs.
  • Limitations of indexes in MongoDBPhoto of Limitations of indexes in MongoDB
    each index not only occupies part of the memory space but also causes an overhead on each insert, update, and delete operation. so, if you rarely use your collection for read operations, you should not use the index for it.