Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats wrong with Mongo DBRef's?

Coming from a RDMBS background it's hard not to think of thinkgs like joins, especially when working with the schema-less MongoDB environemnt.

I read on a blog that DBRefs were only useful when you do know the type of object that you're referencing.

Why is this so? Surely they have more use than that.

Say I have a user collection, and an employer collection. Many users can reference the same employer. To me, this is the perfect use of a DBRef. However, this contradicts what I read on that blog.

Sure, I could embed the employer into each user collection, but what happens when the employer changes? Maybe they employer changes address or phone number or something. If the employer is embedded in each user, then I'd have to update every user's embedded document.

That can't be efficient. Or can it?

like image 467
NeglectedGoldfish Avatar asked Dec 28 '22 16:12

NeglectedGoldfish


1 Answers

DBRefs is a data structure which include a collection name and an object id. If you know the name of you collection (like in your example, employer), you don't need a DBRef. Just store the object id of the employer in your user collection. You save the space taken by the collection name.

Use DBRef when the collection name can change. For example, you have a comment collection. You want to use it to store comments on blog post and on book pages (2 differents collections). If you want to store a reference to the post or the page in your comment, you need to use a database reference.

like image 141
Maxence Avatar answered Jan 06 '23 00:01

Maxence