What is wrong with Mongo DBRef?

Based on the background of RDMBS, it is hard not to think of thinkgs as a union, especially when working with the MongoDB environment without a schema.

I read on the blog that DBRefs were only useful when you know the type of object you are referencing.

Why is this so? Of course, they have more benefit.

Say I have a custom collection and a collection of employers. Many users can link to the same employer. For me, this is the perfect use of DBRef. However, this is contrary to what I read on this blog.

Of course, I could integrate the employer into each user collection, but what happens when the employer changes? Maybe employers change address or phone number or something else. If the employer is embedded in each user, then I will have to update each embedded user document.

It may not be effective. Or maybe it?

+4
source share
1 answer

DBRefs is a data structure that includes the collection name and object identifier. If you know the name of your collection (for example, in your example, the employer), you do not need DBRef. Just save the employer object id in your user collection. You save the space taken by the name of the collection.

Use DBRef when the collection name may change. For example, you have a collection of comments. You want to use it to store comments on your blog and on book pages (2 different collections). If you want to keep a link to a message or page in your comment, you need to use the database link.

+10
source

All Articles