How to query mongodb with DBRef
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
MongoDB’s flexibility and capacity to handle large volumes of unstructured data make it a popular choice for database management systems. Handling references between collections is one of the essential requirements for any database system, and MongoDB provides a unique way to manage them via DBRefs (Database References). In this article, we’ll delve into how to query MongoDB using DBRefs with detailed technical insights and illustrations.
Understanding DBRefs in MongoDB
DBRefs are a way to represent references to documents within different collections in MongoDB. Unlike relational databases that use foreign keys to link tables, MongoDB uses DBRefs to refer to other documents.
A DBRef object typically consists of three fields:
$ref: The name of the collection.$id: The identifier of the document in the referenced collection.$db: An optional field to specify the database name.
Example of DBRef
Here's an example of a document using DBRef:
In this example, the address field is a DBRef pointing to a document within the addresses collection.
Querying Using DBRefs
MongoDB does not automatically resolve DBRefs when querying. Instead, we use the DBRef to perform manual queries on the referenced collection.
Step 1: Retrieve the Base Document
Supposing you have a users collection with the document above, you first query the base document:
This query retrieves the document, including the DBRef.
Step 2: Query the Referenced Document
Next, use the details from the DBRef to access the referenced document in the addresses collection:
This two-step process allows you to retrieve referenced documents manually by using the $id field within the DBRef.
DBRef vs. Manual References
DBRefs are useful for storing referencing information and are preferred when you might need to refer to documents across different databases. However, in many cases, using manual references (by storing the ObjectId directly in the referencing document) is simpler and provides better performance.
Example of Manual Reference
Instead of using DBRefs, a document could simply store the ObjectId:
Retrieving the document would then require:
Advantages and Disadvantages of Using DBRefs
| Feature/Aspect | DBRefs | Manual References |
| Ease of Use | Easier to understand across databases due to formal structure | Simpler and more flexible for single database use |
| Performance | Slower due to additional resolution step | Faster due to direct
ObjectId use |
| Cross-Database | Supported | Not natively supported |
| Indexing | Requires separate indexing
on $ref.$id | Index directly on reference fields |
Conclusion
Using DBRefs in MongoDB offers a structured and portable way to reference documents across collections and databases. While they add a layer of indirection and structure, they aren’t automatically resolved by MongoDB, necessitating manual queries. For applications operating within a single database, manual references are often preferred due to their simplicity and performance benefits. Developers should choose the method best fitting their organizational needs, considering database design, scalability, and performance requirements.
Related reading
- how to rapidly increment counters in Cassandra w/o staleness
- How to re-create database before each test in Spring?
- How to re-sync the Mysql DB if Master and slave have different database incase of Mysql replication?
- How to read from specific instance of a documentdb cluster
- How to read SQL Table data into a C DataTable
- how to rebalance cassandra cluster after adding new node
- How to recover MySQL database from .myd, .myi, .frm files
- How to reduce storage scale down my AWS RDS instance?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.