[MongoDB]: Embedded & Reference relationships

MongoDB Relationships can be demonstrated via Embedded and Referenced approaches and those can be 1:1, 1: N, N: 1 or N: N.

Now let’s see 1:N illustration with below user –> addresses example.

In the EMBEDDED approach, we will embed the address document inside the user document. 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 and the drawback is that if the embedded document keeps on growing too much in size, it can impact the read/write performance.

In REFERENCED Relationships approach (normalized relationship), 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

[Dev root @ dbversitydotcom /opt/mongodb/bin]# ./mongo
MongoDB shell version: 2.6.5
connecting to: test
>
> use dbversity
switched to db dbversity
>
>
> db.address.insert({
… “building”: “EON IT Park”,
… “pincode”: 533250,
… “city”: “East coast”,
… “state”: “AP”,
… “country” : “INDIA”
… })
WriteResult({ “nInserted” : 1 })
>
>
>
> db.address.insert({
… “building”: “WTC towers”,
… “pincode”: 400114,
… “city”: “Malikipuram”,
… “state”: “AP”,
… “country” : “INDIA”
… })
WriteResult({ “nInserted” : 1 })
>
>
> db.address.insert({
… “building”: “Amaravati IT towers”,
… “pincode”: 533253,
… “city”: “Mori”,
… “state”: “AP”,
… “country” : “INDIA”
… })
WriteResult({ “nInserted” : 1 })
>
> show collections
address
system.indexes
>
> db.address.find()
{ “_id” : ObjectId(“552658e334543c481522e328”), “building” : “EON IT Park”, “pincode” : 533250, “city” : “East coast”, “state” : “AP”, “country” : “INDIA” }
{ “_id” : ObjectId(“552658e434543c481522e329”), “building” : “WTC towers”, “pincode” : 400114, “city” : “Malikipuram”, “state” : “AP”, “country” : “INDIA” }
{ “_id” : ObjectId(“552658e634543c481522e32a”), “building” : “Amaravati IT towers”, “pincode” : 533253, “city” : “Mori”, “state” : “AP”, “country” : “INDIA” }
>

> db.users.insert({ user : “dbversit” , dept : “IT” , address_ids : [ ObjectId(“552658e334543c481522e328”), ObjectId(“552658e434543c481522e329”), ObjectId(“552658e634543c481522e32a”) ] })
WriteResult({ “nInserted” : 1 })
>
> db.users.find().pretty()
{
“_id” : ObjectId(“552659cf34543c481522e32b”),
“user” : “dbversit”,
“dept” : “IT”,
“address_ids” : [
ObjectId(“552658e334543c481522e328”),
ObjectId(“552658e434543c481522e329”),
ObjectId(“552658e634543c481522e32a”)
]
}
>
>
>
> show collections
address
system.indexes
users
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({ user : “dbversit” },{ address_ids : 1 , _id : 0})
>
> var addresses = db.address.find( { _id : { $in : result[“address_ids”] } } )
>
>
>
> addresses
{ “_id” : ObjectId(“552658e334543c481522e328”), “building” : “EON IT Park”, “pincode” : 533250, “city” : “East coast”, “state” : “AP”, “country” : “INDIA” }
{ “_id” : ObjectId(“552658e434543c481522e329”), “building” : “WTC towers”, “pincode” : 400114, “city” : “Malikipuram”, “state” : “AP”, “country” : “INDIA” }
{ “_id” : ObjectId(“552658e634543c481522e32a”), “building” : “Amaravati IT towers”, “pincode” : 533253, “city” : “Mori”, “state” : “AP”, “country” : “INDIA” }
>
>
> result
{
“address_ids” : [
ObjectId(“552658e334543c481522e328”),
ObjectId(“552658e434543c481522e329”),
ObjectId(“552658e634543c481522e32a”)
]
}
>

  • Ask Question