[MongoDB]: DBRef (Database references) usage

DBRefs vs Manual References
As an example scenario where we would use DBRefs instead of Manual References, consider a database where we are storing different types of addresses (home, office, mailing, etc) in different collections (address_home, address_office, address_mailing, etc). Now, when a user collection’s document references an address, it also needs to specify which collection to look into based on the address type. In such scenarios where a document references documents from many collections, we should use DBRefs.

Using DBRefs :
There are three fields in DBRefs:

$ref: This field specifies the collection of the referenced document
$id: This field specifies the _id field of the referenced document
$db: This is an optional field and contains name of the database in which the referenced document lies

Consider a sample user document having DBRef field address as shown below:
> 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({
… _id : ObjectId(“552658e334543c481522e328”),
… “address”: { “$ref”: “address_office”,
… “$id”: ObjectId(“552658e334543c481522e328”),
… “$db”: “DBVersity”},
… “name” : “DBVersity_office”,
… “contact” : 099999999999,
… “dob” : “01-01-2015” })
WriteResult({ “nInserted” : 1 })
>
>
>
> db.users.insert({
… _id : ObjectId(“552658e434543c481522e329”),
… “address“: { “$ref”: “address_home”,
… “$id”: ObjectId(“552658e434543c481522e329”),
… “$db”: “DBVersity”},
… “name” : “DBVersity_home”,
… “contact” : 099999999999,
… “dob” : “01-01-2015” })
WriteResult({ “nInserted” : 1 })
>
> db.users.insert({
… _id : ObjectId(“552658e634543c481522e32a”),
… “address“: { “$ref”: “address_mail”,
… “$id”: ObjectId(“552658e634543c481522e32a”),
… “$db”: “DBVersity”},
… “name” : “DBVersity_mailing”,
… “contact” : 099999999999,
… “dob” : “01-01-2015” })
WriteResult({ “nInserted” : 1 })
>
>
1)The address DBRef field here specifies that the referenced address document lies in address_office collection under DBVersity database and has an id of ObjectId(“552658e334543c481522e328”)
2)The address DBRef field here specifies that the referenced address document lies in address_home collection under DBVersity database and has an id of ObjectId(“552658e334543c481522e328”)
3)The address DBRef field here specifies that the referenced address document lies in address_mail collection under DBVersity database and has an id of ObjectId(“552658e634543c481522e32a”)

> db.users.find()
{ “_id” : ObjectId(“552658e334543c481522e328”), “address” : DBRef(“address_office”, ObjectId(“552658e334543c481522e328”)), “name” : “DBVersity_office”, “contact” : 99999999999, “dob” : “01-01-2015” }
{ “_id” : ObjectId(“552658e434543c481522e329”), “address” : DBRef(“address_home”, ObjectId(“552658e434543c481522e329”)), “name” : “DBVersity_home”, “contact” : 99999999999, “dob” : “01-01-2015” }
{ “_id” : ObjectId(“552658e634543c481522e32a”), “address” : DBRef(“address_mail”, ObjectId(“552658e634543c481522e32a”)), “name” : “DBVersity_mailing”, “contact” : 99999999999, “dob” : “01-01-2015” }
>
> db.users.find().pretty()
{
“_id” : ObjectId(“552658e334543c481522e328”),
“address” : DBRef(“address_office”, ObjectId(“552658e334543c481522e328”)),
“name” : “DBVersity_office”,
“contact” : 99999999999,
“dob” : “01-01-2015”
}
{
“_id” : ObjectId(“552658e434543c481522e329”),
“address” : DBRef(“address_home”, ObjectId(“552658e434543c481522e329”)),
“name” : “DBVersity_home”,
“contact” : 99999999999,
“dob” : “01-01-2015”
}
{
“_id” : ObjectId(“552658e634543c481522e32a”),
“address” : DBRef(“address_mail”, ObjectId(“552658e634543c481522e32a”)),
“name” : “DBVersity_mailing”,
“contact” : 99999999999,
“dob” : “01-01-2015”
}
The following code dynamically looks in the collection specified by $ref parameter (address_home in our case) for a document with id as specified by $id parameter in DBRef.
> var user = db.users.findOne({ name: “DBVersity_office” })
>
>
> user
{
“_id” : ObjectId(“55267ec89689e4b8db19b604”),
“address” : DBRef(“address_office”, ObjectId(“552658e334543c481522e328”)),
“name” : “DBVersity_office”,
“contact” : 99999999999,
“dob” : “01-01-2015”
}
>
>
> var dbRef = user.address
>
> dbRef
DBRef(“address_office”, ObjectId(“552658e334543c481522e328”))
>

Now, the below query returns the following address document present in address_home collection:

> db.address.findOne({“_id”:(dbRef.$id)})
{
“_id” : ObjectId(“552658e334543c481522e328”),
“building” : “EON IT Park”,
“pincode” : 533250,
“city” : “East coast”,
“state” : “AP”,
“country” : “INDIA”
}
>

  • Ask Question