This series of posts provides details with examples for modeling MongoDB Manual References and DBRefs by Daprota M2 service. You can access M2 service using this link:
For some data models, it is fine to model data with embedded documents (de-normalized model), but in some cases referencing documents (normalized model) is a better choice.
A referenced document can be
- in the same collection, or
- in a separate collection in the same database, or
- in a separate collection in another database.
MongoDB supports two types of references:
- Manual Reference
Manual References are used to reference documents either in the same collection or in the separate collection in the same database. The parent documents are referenced via the value of their primary key’s _id field.
Database references are references from one document to another using the value of the referenced (parent) document’s _id field, its collection name, and the database name.
In this part of the series we will look at the Manual Reference only. The second part will provide insights into DBRefs.
The Manual Reference MongoDB type indicates that the associated field references another document’s _id. The _id is a unique ID field that acts as a primary key. Manual references are simple to create and they should be used for nearly every use case where you want to store a relationship between two documents.
We will use MongoDB’s Publisher-Book example of the Referenced One-to-Many model. This model comes as a pre-created public model in M2:
Publisher‘s id is of type String and it is referenced in the Book document by the publisher_id field of the type Manual reference:String. This means that the values of the publisher_id field will be referencing the values of the Publisher document _id field.
Now, we will demonstrate how we created this model in M2. We will concentrate only on the Publisher and Book collections creation and the creation of their relevant fields (_id and publisher_id) for this example.
First we will create the Referenced One-to-Many model in M2. Enter the name and description of the model and click the Create Model button to create the model as shown below:
When the model is created, the M2 models page will be loaded and we will click the Referenced One-to-Many model link to load the model’s page:
When the model page is loaded, click the Add Collection tab in order to add the Publisher collection to the model:
Enter the Publisher name and description and click the Add Collection button to create it:
Also create the Book collection.
When both collections are created
we will continue with the Publisher document’s _id field creation. Click the Publisher collection link to load the Publisher collection page and then click the Publisher document link to load the Publisher document page. When the Publisher document page is loaded click the Add Field tab to add the _id field first:
Click the Add Field button to add the field. When the field is added, the Publisher document page will be reloaded:
Click the Full Model View to load the full model view page and then click the Book document link, as depicted below, to load the Book document page:
When the Book document page is loaded, click the Add Field tab to add the publisher_id field. First we will select the Manual Reference as its type:
and then we will add the String as the second part of its composite type which belongs to its values:
Click the Add Field button to add the field. The document page will be reloaded when the field is added:
Click the publisher_id link to load the field page and then click the Manual Reference tab to specify reference details:
When the Manual Reference section is loaded, select the Publisher collection’s document and click the Reference Collection button to complete the Manual reference setup for the publisher_id field:
M2 will create the manual reference and reload the Manual Reference section:
Click the Referenced One-to-Many model link above to load the Referenced One-to-Many model page:
The References section of the page (please see above) lists the reference that was just created. Both the Source (Parent) and Target (Child) column has the format: Collection –> Document –> Field. For example, the Target (Child) column contains the Book –> Book –> publisher_id value which means that Book is the target (child) collection and publisher_id is the field in the Book document of the Book collection whose value will reference the _id field value of the parent Collection (Publisher) document. The Database column is reserved for DBRefs only.
It is also possible that the target (child) document, in the Collection –> Document –> Field value, is not the target collection document but an embedded document (on any level) in the target collection. For example, the Role document in the User –> Role –> _id target reference value in the RBAC model below
is not related to the Role collection but to the Role embedded document of the User document’s roles field:
If you click the embedded Role document’s _id field link, the field page will be loaded with the full path for the _id field: