world! I'm new in mongodb, and I no have idea have I can do this task with aggregation and sum etc..( So, I have this collections: This is stores collection
{
"storeName": "store one",
"_id" : ObjectId("store id 1"), // this is store ID
"shopId" : ObjectId("shopId 1"),
"shopItems" : [
{
"_id" : ObjectId("6048a1fa31d779032b16301e"),
"itemId" : ObjectId("111"), // this is product id from PRODUCTS collection
"itemCount" : 2
},
{
"_id" : ObjectId("6048a46e31d779032b163043"),
"itemId" : ObjectId("222"),// this is product id from PRODUCTS collection
"itemCount" : 0
}
],
},
{
"storeName": "store two"
"_id" : ObjectId("storeId 2"), // this is store ID
"shopId" : ObjectId("shopId 2"),
"shopItems" : [
{
"_id" : ObjectId("6048a1fa31d779032b16301e"),
"itemId" : ObjectId("222"), // this is product id from PRODUCTS collection
"itemCount" : 2
},
{
"_id" : ObjectId("6048a46e31d779032b163043"),
"itemId" : ObjectId("333"),// this is product id from PRODUCTS collection
"itemCount" : -5
}
{
"_id" : ObjectId("6048a46e31d779032b163043"),
"itemId" : ObjectId("111"),// this is product id from PRODUCTS collection
"itemCount" : -7
}
],
}
This is products collection and here is one array with different price, in fact there are many more ~50/50. I have boolean in collection that`s say if the prices for shops is difference (maybe it will be help for task)
// products collection
{
"_id" : ObjectId("111"), // itemId in STORE collection
"differentPricesForShops" : false,
"productName" : "some name here",
"buyPrice" : 10,
"sellingPrice" : 100,
"differentPricesForShops" : false,
"difPriceArray" : []
},
{
"_id" : ObjectId("222"), // itemId in STORE collection
"differentPricesForShops" : false,
"productName" : "some name here",
"differentPricesForShops" : true, <- TRUE
"difPriceArray" : [
{
"shopId": ObjectId("shopId1") <- shop
"buyPrice" : 5, <- differentPrices
"sellingPrice" : 50,
},
{
"shopId": ObjectId("shopId 2")
"buyPrice" : 15,
"sellingPrice" : 55,
},
]
},
{
"_id" : ObjectId("333"), // itemId in STORE collection
"differentPricesForShops" : false,
"productName" : "some name here",
"buyPrice" : 5,
"sellingPrice" : 15,
"differentPricesForShops" : false,
"difPriceArray" : []
},
I need to have this output data:
"shopId" : ObjectId("6048a15031d779032b16300f"),
product count : 2,
product in minus: 0,
selfprice: (2-> count in store * 10(buyPrice) ) = 20
products for sale: (2 -> count in store * 100 (selling price) ) = 200
"shopId" : ObjectId("6048a15031d779032b16300f"),
product count : 2,
product in minus: -5(item count in store) + -7(item count in store) = -12
selfprice: (2-> count in store * 15(different price by shop) ) = 30
products for sale: (2 -> count in store * 55 (different price by shop) ) = 110
this example is by 1 product, but I need to have SUM of selfPrices and products for sale. please, check the image I need to display data like this:
I hope someone can help me. many many thanks!