简体   繁体   中英

How to handle a Many-to-Many relationship with additional fields in MongoDB with Mongoose?

Let's say that I have 2 entities (schemas): Store and Product. A store can have multiple products and a product can be found in multiple stores. I would like to have a quantity attribute / field that would tell me how many products of a sort does a store have. In SQL I would have something like this:

Table Store_X_Product
 » storeId
 » productId
 » quantity

So my question is: how do I create my schemas to handle this case?

You dont store that information usually. When SELECTING, use COUNT function, that way you don't even need to store data. If you really want to store quantity, add a column inside of a store table, but you will need to update it every time you INSERT a product

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM