database - How to design product category and products models in mongodb? -


i new mongo db database design, designing restaurant products system, , design similar simple ecommerce database design each productcategory has products in relational database system 1 (productcategory) many products. have done research understand in document databses. denationalization acceptable , results in faster database reads. therefore in nosql document based database design models way

    //product      {         name:'xxxx',         price:xxxxx,         productcategory:         {           productcategoryname:'xxxxx'          }      } 

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

    //productcategory     {        name:'categoryname',        //array or products        products:[                  {                   name:'xxxx',                   price:xxxxx                   },                    {                   name:'xxxx',                   price:xxxxx                   }                 ]     } 

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

it depends on queries have in mind.

suppose product can belong 1 category, , 1 category applies many products. then, if expect retrieve category product, makes sense store directly:

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

and if expect query products in given category makes sense create separate collection

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

remember cannot atomically update both products , categories database (mongodb eventually consistent), can run batch job make sure categories up-to-date.

i recommend think in terms of what kind of information need, opposed how normalize/denormalize data, , make collections reflect want.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -