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 Document. However, in cases where a Document contains many references from different Collection, we use DBRefs in MongODB.
DBRefs and Manual References
In the following example, 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
You should read it
- MongoDB malicious code attacks more than 26,000 victims in a week
- Learn about security features and authentication in MongoDB
- Advantages of MongoDB
- MongoDB queries and tasks you should know
- Instructions on 2 ways to install MongoDB on Raspberry Pi
- Compare the performance of MongoDB and SQL Server 2008
- Index (Mong) in MongoDB
- Create Database in MongoDB
- Data type in MongoDB
- Query analysis in MongoDB
- How to use Aggregation Pipeline in MongoDB
- Query Document in MongoDB
Maybe you are interested
Automatic document summarization with Copilot is now available in Word
Instructions for downloading documents from Google Docs
Alignment in Word 2010, beautiful alignment of Word 2010 documents
Create Autotext in Word 2007, edit documents faster
Merge and merge multiple documents in Word 2007
Instructions for translating documents in Excel