Learn about DBRefs and Manual References in MongoDB

0
5181
Manual References

In the article “Learn How Data Modelling Works in MongoDB” of this MongoDB series, we discussed about the implementation of the normalized database structure in the MongoDB, where we used the manual references in order to build an embedded database structure. In the referenced relationship, we manually store the referenced document’s id into another document structure to build a relationship between the two documents in the MongoDB. There is another concept known as MongoDB DBRefs which is used when a document contains references to another document present in a different collections. We are going to discuss MongoDB DBRefs in detail in this article.

Comparison between DBRefs vs Manual References
The following data modelling is an example of Manual References, where we have created two separate document structures in the same collection or database ‘REFERENCE_MODEL’.

Document structure for a Blog

Document structure for a Comment

Since both the document structures are present in the same collection, we used the manual references concept in order to embed Comment documents into the Blog document as shown below.

Now consider a case, where we may need to refer to the testimonials of an author of the blog which are present as the documents in a different collection. In such a case, we need to implement the concept of MongoDB DBRefs.

Implementation of the MongoDB DBRefs
In MongoDB, there are three important fields which should be used in order to implement DBRefs relationship as follows.

$ref − this field of MongoDB DBRefs is used to specify the collection of the referenced document.
$id − this field of MongoDB DBRefs is used to specify the _id field of the referenced document.
$db – it is an optional field of MongoDB DBRefs that contains the name of the database in which the referenced document is present.

The following is a sample user document that demonstrates the implementation of MongoDB DBRefs.

Example MongoDB Script for DBRefs concept

Explanation of the above script
In the above script, we have demonstrated both approaches manual references as well as MongoDB DBRefs approach simultaneously.

Firstly, we have inserted a testimonial document in the collection ‘author_testimonial’.
Inside the comments part of the document present in the collection ‘REFERENCE_MODEL’, we have used manual references approach to refer three comments present as a part of Comment document within the same collection or the database through their ObjectId reference. Below is the syntax.

Inside the testimonials part of the document present in the collection ‘REFERENCE_MODEL’, we are using MongoDB DBRefs approach to refer the testimonials present in another collection ‘author_testimonial’ after defining three fields (i.e. $ref, $id and $db) as shown below.

Here, a testimonial will be picked up from the collection ‘author_testimonial’ which has the reference as ‘testimonial’ and ObjectId of the document as ‘ObjectId (“58dc43d6448a4239ed9de434”)’ in this collection.

Conclusion: –
In this chapter, we have revised the concept of manual references used for the data modelling in the MongoDB and compared it against the concept of MongoDB DBRefs, where the former is used when the references are to be made to the documents present in the same collection and the latter approach is used when the references are to be made to the documents present in the different collections.

LEAVE A REPLY

Please enter your comment!
Please enter your name here