In my code, I have a collection called stock and another called product.
Stock holds the data in this format
{
"_id": 123 // id
"product_id": ObjectId ('427875465348') // object id of a product
"quantity": 50 // quantity added in the stock
}
And product Id stores in this format:
{
"_id": "63327c0ba1d239a0dcec7d87", // id used in stock collection
"name": "Rice", // name of product
"category": "Vegetables",
"life": 20,
"price": 22.9
}
Every time a stock is added a new record will be created in stocks table. The stock table can have multiple document with same product id.
I want to display all record of product in this way:
{
"_id": "63327c0ba1d239a0dcec7d87", // id used in stock collection
"name": "Rice", // name of product
"category": "Vegetables",
"life": 20,
"price": 22.9
"quantity" : // sum of all the quantity of the document in stock relating to this product using
product_id
}
How I do that?