MongoDB – Relationships

MongoDB – Relationships

Relationships in MongoDB represent how various documents are logically related to each other. Relationships can be modeled via Embedded and Referenced approaches. Such relationships can be either 1:1, 1:N, N:1 or N:N.
Let us consider the case of storing addresses for users. So, one user can have multiple addresses making this a 1:N relationship.
Following is the sample document structure of user document −

{
"_id" : ObjectId("602372d4f7661885eff39c92"),
"userId" : "rirani",
"jobTitleName" : "Developer",
"firstName" : "Romin",
"lastName" : "Irani",
"preferredFullName" : "Romin Irani",
"employeeCode" : "E1",
"region" : "CA",
"phoneNumber" : "408-1234567",
"emailAddress" : "romin.k.irani@gmail.com"
}

Following is the sample document structure of address document −

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

Modeling Embedded Relationships

In the embedded approach, we will embed the address document inside the user document.

> db.empFullDetails.insert(
{
"_id" : ObjectId("602372d4f7661885eff39c92"),
"userId" : "rirani",
"jobTitleName" : "Developer",
"firstName" : "Romin",
"lastName" : "Irani",
"preferredFullName" : "Romin Irani",
"employeeCode" : "E1",
"region" : "CA",
"phoneNumber" : "408-1234567",
"emailAddress" : "romin.k.irani@gmail.com",
"Addresses":[
{
"building": "23 B",
"pincode": 456789,
"city": "Los Angeles",
"state": "California"
},
{
"building": "160 B",
"pincode": 456789,
"city": "Chicago",
"state": "Illinois"
}]}
)
WriteResult({ "nInserted" : 1 })

This approach maintains all the related data in a single document, which makes it easy to retrieve and maintain. The whole document can be retrieved in a single query such as −

> db.empFullDetails.findOne({"userId":"rirani"},{"Addresses":1})
{
"_id" : ObjectId("602372d4f7661885eff39c92"),
"Addresses" : [
{
"building" : "23 B",
"pincode" : 456789,
"city" : "Los Angeles",
"state" : "California"
},
{
"building" : "160 B",
"pincode" : 456789,
"city" : "Chicago",
"state" : "Illinois"
}
]
}
>

Note that in the above query, db and users are the database and collection respectively.
The drawback is that if the embedded document keeps on growing too much in size, it can impact the read/write performance.

Modeling Referenced Relationships

This is the approach of designing normalized relationship. In this approach, both the user and address documents will be maintained separately but the user document will contain a field that will reference the address document's id field.

{
"_id" : ObjectId("602372d4f7661885eff39c92"),
"userId" : "rirani",
"jobTitleName" : "Developer",
"firstName" : "Romin",
"lastName" : "Irani",
"preferredFullName" : "Romin Irani",
"employeeCode" : "E1",
"region" : "CA",
"phoneNumber" : "408-1234567",
"emailAddress" : "romin.k.irani@gmail.com",
"Addresses" : [
ObjectId("60238fd9487b4061762d4501"),
ObjectId("60238fd9487b4061762d4502")
]
}

As shown above, the user document contains the array field address_ids which contains ObjectIds of corresponding addresses. Using these ObjectIds, we can query the address documents and get address details from there. With this approach, we will need two queries: first to fetch the address_ids fields from user document and second to fetch these addresses from address collection.

>var result = db.users.findOne({"name":"Tom Benzamin"},{"address_ids":1})
>var addresses = db.address.find({"_id":{"$in":result["address_ids"]}})
MongoDB – PHP (Prev Lesson)
(Next Lesson) MongoDB – Database References