I am faily new to NoSql,i am working on a project and need to use Nodejs with mongodb, i read alot but can't seem to figure it out.The app will have categories and items with one to many relationship,should i use 1 collection where category object has an array of products. or use 2 collections (category and products)
CodePudding user response:
If there are going to be a lot of products for a category, and you store the category with an array of products, there are high chances that it may become an unbounded array as the number of products increase for a category, and would impact the database's overall performance. It is always a better idea to avoid having unbounded arrays in your schema. Thus, it would be better to have 2 collections, one for category and another for products (with a field called categoryId). You can read more about the best practices around modelling One-to-Many relationships here.