Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

Database Reference in Mongodb: Reference Database in Mongodb

Understand Database Reference in Mongodb: Reference Database in Mongodb with clear explanations, practical examples, and useful tips. This updated guide...

Table of Contents

Database Reference in Mongodb: Reference Database in Mongodb is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.

As shown in the Relationship chapter in MongoDB, to deploy a standardized database structure in MongoDB, we use the Referenced Relationship concept, also known as Manual References , in which we manipulate to store the id of the Documents referenced in another Document. However, in cases where a Document contains many references from different Collection, we use DBRefs in MongODB.

DBRefs and Manual References

In the example below, we will use DBRefs instead of Manual References. Suppose there is a database we use to store different types of addresses (home, office, mailing,.) in different collections (address_home, address_office, address_mailing,.). Now, when a user collection references an address, it also needs to determine which collection it will look at, based on the address type. In such a situation, when a Document references another Document from multiple Collection, we should use DBRefs.

Use DBRefs in MongoDB

There are 3 fields in DBRefs:

$ ref : This field identifies the Collection of the Document referenced.

$ id : This field identifies the _id field of the Document referenced.

$ db : This field is an arbitrary field, containing the name of the Database to which the Document is referenced.

Suppose a user document has an address field of DBRefs as follows:

 { "_id" : ObjectId ( "53402597d852426020000002" ), "address" : { "$ref" : "address_home" , "$id" : ObjectId ( "534009e4d852427820000002" ), "$db" : "tutorialspoint" }, "contact" : "987654321" , "dob" : "01-01-1991" , "name" : "Tom Benzamin" } 

The address field of the DBRefs format here specifies that the address document is referenced in address_home collection under the tutorialspoint database and has an id of: 534009e4d852427820000002.

The following code will look at the Collection defined by the parameter $ ref (which is address_home in this case) for a Document with id defined by the parameter $ id in DBRefs.

 > var user = db . users . findOne ({ "name" : "Tom Benzamin" }) > var dbRef = user . address > db [ dbRef . $ref ]. findOne ({ "_id" :( dbRef . $id )}) 

The above code returns the following address document , which is present in the address_home collection:

 { "_id" : ObjectId ( "534009e4d852427820000002" ), "building" : "22 A, Indiana Apt" , "pincode" : 123456 , "city" : "Los Angeles" , "state" : "California" } 

According to Tutorialspoint

Previous post: Relationship in MongoDB

Next lesson: Covered Query in MongoDB

FAQ

What should you know about dBRefs and Manual References?

In the example below, we will use DBRefs instead of Manual References. Suppose there is a database we use to store different types of addresses (home, office, mailing,.) in different collections (address_home, address_office, address_mailing,.). Now, when a.

What is Database Reference in Mongodb: Reference Database in Mongodb?

As shown in the Relationship chapter in MongoDB, to deploy a standardized database structure in MongoDB, we use the Referenced Relationship concept, also known as Manual References, in which we manipulate to store the id of the Documents referenced in another.

Why is Database Reference in Mongodb: Reference Database in Mongodb important?

A clear understanding of Database Reference in Mongodb: Reference Database in Mongodb helps you make informed decisions, avoid common mistakes, and use the relevant tools or techniques more effectively.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.