【发布时间】:2021-08-23 00:26:15
【问题描述】:
世界! 我是 mongodb 的新手,我不知道我可以通过聚合和求和等来完成这项任务..( 所以,我有这个系列: 这是商店集合
{
"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
}
],
}
这是产品集合,这是一个不同价格的数组, 实际上还有更多〜50/50。我收集了 boolean 表示商店的价格 是不同的(也许对任务有帮助)
// 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" : []
},
我需要这个输出数据:
"shopId" : ObjectId("6048a15031d779032b16300f"),
产品数量:2,
产品负数:0,
selfprice: (2-> 店内计数 * 10(buyPrice) ) = 20
待售产品:(2 -> 店内数量 * 100(售价))= 200
"shopId" : ObjectId("6048a15031d779032b16300f"),
产品数量:2,
减号产品:-5(店内商品数)+ -7(店内商品数)= -12
selfprice:(2-> 店内数 * 15(不同店价))= 30
待售产品:(2 -> 店内数量 * 55(各店价格不同))= 110
这个例子是按 1 个产品,但 我需要有 selfPrice 和待售产品的总和。 请检查图片 我需要这样显示数据:
我希望有人可以帮助我。非常感谢!
【问题讨论】:
标签: typescript mongodb mongoose aggregation