我是一个新的堆栈溢出问题。我在组 itemId 中存在一些问题,总入入和出出数量与下面的 aggregate mongodb 获得新的余额数量是我的收藏店。我按 itemId 和 purchaseOrderDetailId
分组我的收藏集
let collection=[{
"_id" : "011-0",
"tranDate" : ISODate("2019-03-10T09:36:48.971+07:00"),
"refType" : "Purchase_Order",
"purchaseOrderId" : "011",
"purchaseOrderDetailId" : "0110",
"refId" : "011",
"itemId" : "63",
"qty" : 10,
"cost" : 13,
},
{
"_id" : "011-1",
"tranDate" : ISODate("2019-03-10T09:36:48.971+07:00"),
"refType" : "Purchase_Order",
"purchaseOrderId" : "011",
"purchaseOrderDetailId" : "0111",
"refId" : "011",
"itemId" : "63",
"qty" : 5,
"cost" : 0,
},
{
"_id" : "01123-0-",
"refType" : "Cash_Purchase",
"refId" : "01123",
"purchaseOrderId" : "011",
"purchaseOrderDetailId" : "0110",
"tranDate" : ISODate("2019-03-10T09:37:51.080+07:00"),
"itemId" : "63",
"qty" : -10,
"cost" : 13,
},
{
"_id" : "01123-1-",
"refType" : "Cash_Purchase",
"refId" : "01123",
"purchaseOrderId" : "011",
"purchaseOrderDetailId" : "0111",
"tranDate" : ISODate("2019-03-10T09:37:51.080+07:00"),
"itemId" : "63",
"qty" : -5,
"cost" : 0,
}]
我想要这样的汇总数据,请帮忙
{
"_id" : "011-1",
"refId" : "01123",
"purchaseOrderId" : "011",
"purchaseOrderDetailId" : "0111",
"tranDate" : ISODate("2019-03-10T09:37:51.080+07:00"),
"itemId" : "63",
"qty" : 5,
"qtySub" :-5,
"balQty":0
}
{
"_id" : "011-0",
"tranDate" : ISODate("2019-03-10T09:36:48.971+07:00"),
"purchaseOrderId" : "011",
"purchaseOrderDetailId" : "0110",
"refId" : "011",
"itemId" : "63",
"qty" : 10,
"qtySub" :-10,
"balQty":0
}