How to use relations between documents and collections in MongoDB How to use relations between documents and collections in MongoDB mongoose mongoose

How to use relations between documents and collections in MongoDB


There are two ways to do that, using Manual References, which you are already doing, or using DBRefs, which is just a convention for representing a document, but is supported by the MongoDB Drivers, as you can see in this example.

Manual references, however, don't have any kind of special support natively, so you have two options to populate those references, which I guess is what you mean with bind these two collections.

  • The first one is to do that manually using the MongoDB Driver directly from your application, that is, you query the parent document or documents, read their references, and query the additional documents. Then you can merge all those documents together to return a single result.

  • The second option, which I think it's better, is to rely on a librery of your choice to do that for you. I would recommend taking a look at Mongoose if you are using Node.js.

    Mongoose let's you easily define references between documents and populate them easily, as you can see the official docs.

I would probably choose Manual References because I think it is more concise and there's no need to store schema-related information in the database (which takes space), but that's maybe a matter of personal preference, although it is also the recommended option:

Unless you have a compelling reason to use DBRefs, use manual references instead.

DBRefs Example:

One of the main differences is that when using DBRefs, all the information of the relationship is stored in the document itself, so you may have a posts collection whose documents looks like this:

{    "_id" : ObjectId("5126bbf64aed4daf9e2ab771"),    "text" : ...,    "author" : {        "$ref" : "authors",        "$id" : ObjectId("5126bc054aed4daf9e2ab772"),        "$db" : "users"    }}

Just by looking at that document, you know where the author data is located. You can see it is stored in a DB called users, in a collection called authors and that its _id is 5126bc054aed4daf9e2ab772.

Manual Reference Example:

If you where using Manual References, however, the same document will look like this:

{    "_id" : ObjectId("5126bbf64aed4daf9e2ab771"),    "text" : ...,    "author" : ObjectId("5126bc054aed4daf9e2ab772")}

So just by looking at that document you can not know where to find the information needed to populate the author field. That information will now reside in the application level, in Mongoose's schema definition particularly if you choose to use this tool.

Performance Note:

In both cases, you will be doing additional requests to populate those references, so DBRefs is not giving you any kind of performance gain over manual references.

Embedding / Denormalisation Note:

Embedding documents and having indexes on them if needed is what will give you more performance gains if done wisely and guarantees you have atomic operations, as MongoDB offers single-document atomicity.