6

I've been researching MongoDB and trying to see if it would be a fit for my company's product catalog.

I came across database references: http://docs.mongodb.org/manual/reference/database-references/ and had some questions about how exactly to link documents together.

Assume we have two db.categories and db.products collections... I have documents in db.products that need to link to their categories in db.categories.

I have a "category" field in db.products that I will store the objectID of the category document in. However, I do not see a way to specify that this objectID references the db.categories collection. Do I need to use DBREFs?

Below is a brief example of my data...

Document in db.categories:

 {
        "_id" : ObjectId("546b72a47ea1d03593000158"),
        "name" : "Category1",
        "type" : "type1 of category1",
        "spec_columns" : [ 
            {
                "field" : "capacity",
                "display" : "Capacity (tons)"
            }, 
            {
                "field" : "weight",
                "display" : "Weight (lbs)"
            }
        ]
    }

Document in db.products:

{
    "_id" : ObjectId("546a575f7ea1d03593000154"),
    "model" : "NER003H",
    "category" : ObjectId("546b72a47ea1d03593000158"),
    "capacity" : "1/2",
    "weight" : 10
}

Any help, suggestions and advice is welcome and greatly appreciated.

Darius

12

Short version: use a manual reference, not a DBRef.

Explanation:

There is no particular benefit to the use of a DBRef beyond giving you the collection and database that the referenced document resides in. If you know the reference is only between categories and products, then that is not particularly useful. A DBRef simply contains the _id of the referenced document, the collection name and optionally the database name. You will usually have all that information in your application (i.e. you have the _id and you know that db/collection it resides in already).

If that were not the case, let's say you had references to documents in a slew of databases and collections and it was difficult or cumbersome application side to keep track of which one each document resided in, then it may be a benefit to use DBRefs. After all, the easiest way to do a manual reference in that case would be to store the database and collection that you are referencing in the document too, and that is what DBRefs do for you in any case.

That (referencing multiple collections from a single source collection) is not a terribly common pattern though and not what you have outlined above, and so unless you have such a compelling reason to use them, manual references are recommended instead.

Some of the drivers have helper methods which may save a few lines of code versus a manual reference, but not all drivers do, and it may make the code harder to understand (without some nice comments/documentation to explain it). As well as that, the DBREF approach is more fixed than a manual implementation - what if you want to reference a field other than _id for example.

Finally, in terms of product catalog design, I would highly recommend reading these two excellent and detailed blog posts from Antoine Girbal:

There were also three presentations given at MongoDB World 2014 on this very topic, so I would recommend reviewing those also as you progress:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.