Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

MongoDB relationship


May 17, 2021 MongoDB


Table of contents


MongoDB relationship

The relationship between MongoDB represents the logical interconnectedity of multiple documents.

Documents can be linked by embedding and referencing.

Relationships in MongoDB can be:

  • 1:1 (1 to 1)
  • 1: N (1-to-many)
  • N: 1 (many to 1)
  • N: N (many-to-many)

Let's consider the relationship between the user and the user's address.

A user can have multiple addresses, so it is a one-to-many relationship.

Here's a simple structure of the user document:

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

Here's a simple structure of the address document:

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

Embedded relationships

Using the embedded method, we can embed the user's address into the user's 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"
      }]
} 

The above data is stored in a single document and can be easily obtained to maintain the data. You can query the user's address like this:

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

Note: Db and users in the above query represent databases and collections.

The disadvantage of this data structure is that if the user and user address are increasing and the amount of data is getting erring, it can affect read and write performance.

A reference relationship

Reference relationships are a method often used when designing a database, which separates the user data document from the user address data document and establishes the relationship by referencing the document's id field.

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

In the above example, the object address_ids of the user document contains an array of object ids for the user's address.

We can read the object id (ObjectId) of these user addresses to get the user's detailed address information.

This method requires two queries, the first query for the user's address object id (ObjectId), and the second query id for the user's detailed address information.

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