- MongoDB - PHP
- MongoDB - Java
- MongoDB - Deployment
- MongoDB - Create Backup
- MongoDB - Sharding
- MongoDB - Replication
- MongoDB - Aggregation
- MongoDB - Indexing
- MongoDB - Sorting Records
- MongoDB - Limiting Records
- MongoDB - Projection
- MongoDB - Delete Document
- MongoDB - Update Document
- MongoDB - Query Document
- MongoDB - Insert Document
- MongoDB - Data Types
- MongoDB - Drop Collection
- MongoDB - Create Collection
- MongoDB - Drop Database
- MongoDB - Create Database
- MongoDB - Data Modeling
- MongoDB - Environment
- MongoDB - Advantages
- MongoDB - Overview
- MongoDB - Home
Advanced MongoDB
- Auto-Increment Sequence
- MongoDB - Capped Collections
- MongoDB - GridFS
- Working with Rockmongo
- MongoDB - Regular Expression
- MongoDB - Text Search
- MongoDB - Map Reduce
- MongoDB - ObjectId
- MongoDB - Indexing Limitations
- MongoDB - Advanced Indexing
- MongoDB - Atomic Operations
- MongoDB - Analyzing Queries
- MongoDB - Covered Queries
- MongoDB - Database References
- MongoDB - Relationships
MongoDB Useful Resources
- MongoDB - Discussion
- MongoDB - Useful Resources
- MongoDB - Quick Guide
- MongoDB - Questions and Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
MongoDB - Database References
As seen in the last chapter of MongoDB relationships, to implement a normapzed database structure in MongoDB, we use the concept of Referenced Relationships also referred to as Manual References in which we manually store the referenced document s id inside other document. However, in cases where a document contains references from different collections, we can use MongoDB DBRefs.
DBRefs vs Manual References
As an example scenario, where we would use DBRefs instead of manual references, consider a database where we are storing different types of addresses (home, office, maipng, etc.) in different collections (address_home, address_office, address_maipng, etc). Now, when a user collection s document references an address, it also needs to specify which collection to look into based on the address type. In such scenarios where a document references documents from many collections, we should use DBRefs.
Using DBRefs
There are three fields in DBRefs −
$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 pes
Consider a sample user document having DBRef field address as shown in the code snippet −
{ "_id":ObjectId("53402597d852426020000002"), "address": { "$ref": "address_home", "$id": ObjectId("534009e4d852427820000002"), "$db": "tutorialspoint"}, "contact": "987654321", "dob": "01-01-1991", "name": "Tom Benzamin" }
The address DBRef field here specifies that the referenced address document pes in address_home collection under tutorialspoint database and has an id of 534009e4d852427820000002.
The following code dynamically looks in the collection specified by $ref parameter (address_home in our case) for a document with id as specified by $id parameter in DBRef.
>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 present in address_home collection −
{ "_id" : ObjectId("534009e4d852427820000002"), "building" : "22 A, Indiana Apt", "pincode" : 123456, "city" : "Los Angeles", "state" : "Capfornia" }Advertisements