MongoDB for everyone – Part 8 – dbrefs and denormalization

Most of the MongoDB drivers (as well as the database engine itself) supports a concept called “dbrefs”. Basically, what a dbref is, is a reference to another document in a different collection. A dbref will have two pieces of information attached to it, an ID of the document that it references, as well as the collection that it is in.

Many ORM’s and abstraction layers that you may use within your own projects will create dbrefs by default, some may be configured not to. If at all possible, I would suggest avoiding dbrefs as much as possible, and rather use denormalization techniques to make use of larger documents in your collections. Remember, that you are now working with documents and not relationships, so that in bred fear of data duplication should be relaxed somewhat!

That being said, however, you should model your data to your application usage. Take, for instance a blog, with comments. You could have a collection for users, comments, and posts with many dbrefs, or you could stick the entire post into a single document (comments included!) That may be a bit extreme though, so consider using adding a user name field for any commenters inside of your blog document, as opposed to just a user ID. That way, and without adding very much data to your blog document, you avoid a lookup on a dbref and make your application faster! Win!

Thinking denormalised may take some getting used to, but it works well with document centric data stores. Just make sure you don’t hit those 16MB document size limits!



Author Rating
Software Name
Landing Page

Liked this post? Follow this blog to get more.