组itemId求和进出总和以查找余额剩余新数量?

时间:2019-03-10 07:01:45

标签: arrays mongodb meteor vuejs2 aggregate

我是一个新的堆栈溢出问题。我在组 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
  }

0 个答案:

没有答案