Navicat Blog

Feb 19, 2019 by Robert Gravelle

In last week's blog, we explored the pros and cons of document relationship modeling via Embedded and Referenced approaches in MongoDB. We then gained some valuable experience with each by creating both an Embedded and Referenced relationship. Today, we'll learn how to create DBRefs in MongoDB.

Comparing DBRefs to Referenced Relationships

As we saw in last week's blog on MongoDB relationships, we can implement a normalized database structure in MongoDB by creating a Referenced Relationship. Referenced Relationships are also often referred to as Manual References because we manually store the referenced document's id (or entire document) inside the other document. In cases where a document contains references from different collections, we can use MongoDB DBRefs.

As an example scenario, where we would use DBRefs instead of manual references, let's consider a scenario 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 up based on the address type. Whenever a document needs to reference documents from many collections, we should use DBRefs.

DBRefs in Action

DBRefs are made up of three fields:

  • $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 the name of the database in which the referenced document lies.

Let's modify Barbara Palmer's address information from last week's blog by removing the embedded fields and replacing them with DBRefs.

  • Open her information in the Navicat Editor in "Tree View". Expand the first address document and click on the plus (+) sign below the address fields to insert the three DBRef fields above:

  • The DBRef requires the address document's $id field. You can located it in the addresses collection. Just copy it form the "135 Sycamore Dr." address document:

  • Once you're done adding the new fields, be sure to delete all of the existing address information from the document:

  • Here's the completed address_home DBRef:

  • Follow the same procedure for Barbara Palmer's second address, except this time, assign it a $ref of "address_work".

Using the New DBRefs

Now that we've updated our an employee document to use DBRefs, we will have to alter how we go about fetching the data from the employees collection. Here's some code that dynamically looks in the collection specified by $ref parameter (address_home in our case) for a document with the id specified by the DBRef $id parameter:


In today's blog, we learned how to create DBRefs in MongoDB using Navicat for MongoDB. DBRefs are great for linking documents located in multiple collections with documents from a single collection. Having said that, if you don't have documents referred to in multiple collections, I would recommend sticking with manual references.

If you'd like to learn more about Navicat for MongoDB, please visit the product page. Do you work with many database types? Navicat Premium 12.1 also supports MongoDB!

Navicat Blogs
Feed Entries
Blog Archives