Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to design product category and products models in mongodb?

I am new to mongo db database design, I am currently designing a restaurant products system, and my design is similar to a simple eCommerce database design where each productcategory has products so in a relational database system this will be a one (productcategory) to many products. I have done some research I do understand that in document databses. Denationalization is acceptable and it results in faster database reads. therefore in a nosql document based database I could design my models this way

    //product
     {
        name:'xxxx',
        price:xxxxx,
        productcategory:
        {
          productcategoryName:'xxxxx'

        }
     }

my question is this, instead of embedding the category inside of each product, why dont we embed the products inside productcategory, then we can have all the products once we query the category, resulting in this model.

    //ProductCategory
    {
       name:'categoryName',
       //array or products
       products:[
                 {
                  name:'xxxx',
                  price:xxxxx
                  },
                   {
                  name:'xxxx',
                  price:xxxxx
                  }
                ]
    }

I have researched on this issue on this page http://www.slideshare.net/VishwasBhagath/product-catalog-using-mongodb and here http://www.stackoverflow.com/questions/20090643/product-category-management-in-mongodb-and-mysql both examples I have found use the first model I described (i.e they embed the productCategory inside product rather than embed array of products inside productCategory), I do not understand why, please explain. thanks

like image 922
Yusuf Kelo Avatar asked Jul 08 '15 19:07

Yusuf Kelo


People also ask

How do I create a collection schema in MongoDB?

Basically, MongoDB is schema-less database, we cannot create schema in MongoDB, but we enforce the collection documents in application code or using MongoDB atlas GUI tool. For generating schema first we need to connect to the specified database and collections in MongoDB.

Can I use MongoDB for ecommerce?

It simplifies data integration and offers better scalability than traditional relational databases. Today's e-commerce sites need rich data models and dynamic queries. MongoDB provides this, making it a popular choice for many companies. There's a wide range of features you can build out for your store using MongoDB.


2 Answers

For the DB design, you have to consider the cardinality (one-to-few, one-to-many & one-to-gazillions) and also your data access patterns (your frequent queries, updates) while designing your DB schema to make sure that you get optimum performance for your operations. From your scenario, it looks like each Product has a category, however it also looks like you need to query to find out Products for each category. So, in this case, you could do with something like :

Product = { _id = "productId1", name : "SomeProduct", price : "10", category : ObjectId("111") }
ProductCategory = { _id = ObjectId("111"), name : "productCat1", products : ["productId1", productId2", productId3"]}

As i said about data access patterns, if you always read the Category-name and "Category-name" is something which is very infrequently updated then you can go for denormalizing with this two-way referencing by adding the product-category-name in the product:

Product = { _id = "productId1", name : "SomeProduct", price : "10", category : { ObjectId("111"), name:"productCat1" }

So, with embedding the documents, specific queries would be faster if no joins would be required, however other queries in which you need to access embedded details as stand-alone entities would be difficult.

This is a link from MongoDB which explains DB design for one-to-many scenario like you have with very nice examples in which you would realize that there are much more ways of doing it and many other things to think about.

http://blog.mongodb.org/post/88473035333/6-rules-of-thumb-for-mongodb-schema-design-part-3 (also has links for parts 1 & 2) This link also describes the pros & cons for each scenario enabling you to reach a narrow down on a DB schema design.

like image 193
anish Avatar answered Sep 29 '22 12:09

anish


It all depends on what queries you have in mind.

Suppose a product can only belong to one category, and one category applies to many products. Then, if you expect to retrieve the category together with the product, it makes sense to store it directly:

 // products
 {_id:"aabbcc", name:"foo", category:"bar"}

and if you expect to query all the products in a given category then it makes sense to create a separate collection

// categories
{_id:"bar", products=["aabbcc"]}

Remember that you cannot atomically update both the products and categories database (MongoDB is eventually consistent), but you can occasionally run a batch job which will make sure all categories are up-to-date.

I would recommend to think in terms of what kind of information will I often need, as opposed to how to normalize/denormalize this data, and make your collections reflect what you actually want.

like image 24
Escualo Avatar answered Sep 29 '22 12:09

Escualo