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

  • Text Search in MongoDBText Search in MongoDB
    starting with version 2.4, mongodb started supporting text indexes to search within the string content.
  • How to Slow Down a RelationshipHow to Slow Down a Relationship
    sometimes, you might feel like a relationship is moving too quickly – whether that means physically or emotionally. it's important to remember that a relationship is an agreement between two people. you don't need to go along with...
  • Shard in MongoDBShard in MongoDB
    sharding is a process of storing data records across multiple devices and it is a method of mongodb to meet the requirement for increasing data. when the size of the data increases, a single device cannot be enough to store data.
  • Learn about Java Driver in MongoDBLearn about Java Driver in MongoDB
    in the following article, we will introduce you some basic features of mongodv java driver as well as how to deploy and apply in practice.
  • Install MongoDBInstall MongoDB
    instructions for installing mongodb on windows.
  • Map Reduce in MongoDBMap Reduce in MongoDB
    in mongodb documentation, map-reduce is a data processing system that condenses a large amount of data into useful overall results. mongodb uses mapreduce command for map-reduce operation. in general, map reduce is used to handle large data sets.
  • Aggregation in MongoDBAggregation in MongoDB
    aggregation can be understood as aggregation. the aggregation operation handles data records and returns calculated results. the operations group the values ​​from multiple documents together, and can perform multiple operations on the grouped data to return a single result. in sql, count (*) and group by are equivalent to aggregation in mongodb.
  • Data modeling in MongoDBData modeling in MongoDB
    data in mongodb has a flexible schema. documents in the same collection need not have the same set of fields or structures, and common fields in collection documents can keep different data types.
  • How to Make a Relationship BetterHow to Make a Relationship Better
    after all the effort it takes to get into a quality relationship, it may seem like the hard work is over once you start dating someone. however, a healthy relationship is like a living, breathing thing — it takes constant care and...
  • How to use Aggregation Pipeline in MongoDBHow to use Aggregation Pipeline in MongoDB
    if you are using mongodb's mapreduce, it is best to switch to aggregation pipeline for more efficient computation.