Reputation: 10629
Trying to get my hands dirty with MongoDB, coming from a relational database background.
I believe one of the main concepts of MongoDB is to keep as much data together as possible.
Imagine I create an application that have products and categories, and I store products this way:
Products collection
{
"_id": "30671", //main item ID
"department": "Shoes",
"category": "Shoes/Women/Pumps",
"brand": "Calvin Klein",
"thumbnail": "http://cdn.../pump.jpg",
"title": "Evening Platform Pumps",
"description": "Perfect for a casual night out or a formal event.",
"style": "Designer",
…
}
Categories collection
{
"_id": "12356",
"name": "Shoes"
…
}
If I update a category name, would I need to run a huge update command to update all products as well? Or in this scenario it would be better to actually store the category ids against the products instead of their names (like we would do on a relational database)?
Thanks
Upvotes: 0
Views: 89
Reputation: 13775
In general there is no "one-size-fits-all" approach in MongoDB like it is in relational database world. In a relational schema design, it is a no-brainer to put Category
in a separate table. In fact, it is almost a requirement to do so during normalization process.
In MongoDB, schema design almost entirely depends on what you need and your use case more than any rule-of-thumb or any formulated requirements. Of course, there are pluses/minuses for each choice. For example:
Category
doesn't change much (or at all), then you can safely put it inside the Products
collection. However, should you need to rename a category, then yes you would need to update all the products belonging to that category.I noticed that you used "category": "Shoes/Women/Pumps"
in your example. In MongoDB, you can put that into an array if your use case allows it, e.g. "category": ["Shoes", "Women", "Pumps"]
. This may make the field easier to index (again, depending on your use case).
In short, there is no right or wrong in MongoDB schema design with one caveat: usually the wrong thing to do is trying to emulate a relational design in MongoDB, since it goes against the grain.
You also may find these links helpful:
Upvotes: 1