DBref usage in mongodb

Hi team ,

Need help for usage of ‘DBref’ or $ref clause in documents. As we know these clauses used for referencing one document to other in mongodb.

from some blogs and mongodb manuals I got to know this approach is not very helpful for query
performence and should be avoided.


Can you please suggest , instead of DBref what other alertnative approach we can use for better query performence.

Kind regards

Hi @Gaurav_Gupta, if you are not sharding your data and the collections are in the same database you can look into $lookup (available since MongoDB 3.2) or $graphLookup (available since MongoDB 3.4) depending on your needs.

Also can you data be modeled in a way that doesn’t require joins between collections? If it can that will also give data retrieval a performance boost. Looking at the MongoDB Data Modeling and Memory Sizing and the Building with Patterns blog posts can help with looking into different ways to build out the data models for different use cases.

1 Like

Hi Duncan

Thanks so much :blush: – I will check those options. I don think our environment uses sharding. From somewhere on web I got know ‘Manual linking’ could also be helpful over DBref …

but it can only be used in same documents of collections. If we need to link documents in different collections .
Then have to use DBref only.

Does manual linking has any limitations ? How to fix this if we have documents on different collections.

Hi @Gaurav_Gupta,

As @Doug_Duncan mentioned, definitely consider whether referencing is the best path for your data model and use case. This approach often feels comfortable coming from an RDBMS data modelling background, but may not be taking advantage of some of the flexibility that MongoDB enables. For best performance and outcomes with MongoDB, you need to focus on modelling to support your common application usage rather than strictly normalising data.

If you do want to relate documents (which can definitely be appropriate), I strongly recommend using manual referencing. This approach provides the most flexibility if you are likely to want to perform additional manipulation in future (such as using $lookup and other aggregation operators).

DBRefs use an older convention which represents references using a document format:

{ "$ref" : <value>, "$id" : <value>, "$db" : <value> }

The DBRef convention uses $-prefixed keys and generally has limited support in modern drivers, tools, and aggregation queries. The convention isn’t officially deprecated, but isn’t a great choice for modern applications.

In addition to the Building with Patterns series of blog posts mentioned in Doug’s earlier comment , I would also consider taking some of the free online courses at MongoDB University. There’s a MongoDB for Developers Learning Path with some course recommendations, or you could also dive into courses like M320: Data Modelling if you already have some MongoDB experience.


1 Like

Hi Stennie ,

Thanks so much for suggestions :slight_smile: - Will certainly work on that, will get back if further any queries.

Thanks again !
kind regards

1 Like

Hello, I’v read somewhere on this page https://docs.mongodb.com/manual/reference/database-references/#manual-references, some limitations of using manual referencing.

In my case, I’m the modeling phase of my database. And many collections are related through documents. If I use manual reference by considering what I read on this page, the entire document isn’t conveyed to other collection. I’m still thinking if I should change my manual ref with DBRefs.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.