I have a collection of store products, where each document is a product that is associated with a store and their locations, in addition to the quantity of that product for that store. The document schema looks as follows:
Sample Doc :
{
product : 'Mobile Phone',
store : 'Electronics Store',
total_quantity : 5,
locations : [ { id: 'loc1', quantity: 3 }, { id: 'loc2', quantity: 2 } ]
}
I want to be able to remove a location from all the store products by the location id, while also updating the total quantity.
I know I can do this by getting each document and updating it, but this requires queries equal to the amount of the products that has that location. Hence, I was thinking of trying to achieve this by doing two queries:
- An update query the matches the location id and decrements the total quantity based on it in one go
- A remove query that pulls the location from the locations array
The problem here is I do not know how to achieve the first step or if it is even possible. Any other suggestions are more than welcome.