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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With