我似乎找不到解决这个问题的办法。我就是不知道怎么了。
我已经试过了,但没有希望。这篇文章也没有进一步解释。
这是我的代码:

 var aggregateOptions;
  aggregateOptions = [{
    $match: {

    }
  },{
    $group: {
      _id: "$customerID",
      totalAmount: { $sum: "$amount" },
      totalpaidAmount: { $sum: "$paidAmount" },
      totalAmountDue: { $subtract: ["$totalAmount", "totalpaidAmount"] }
    }
  }];

  Sale.sales.get(Sale.Schema, aggregateOptions, (err, saleRecord) => {
    if (err) throw err;
    console.log(saleRecord);

    res.json({
      pageTitle: "Customer List",
      currentUser: req.user,
      saleRecord: saleRecord,
    });
  });

它提示mongoerror:每次查询$subtract累加器时,它都是一元运算符。

最佳答案

将查询部分更改为此部分:

aggregateOptions = [{
$match: {

}
},{
$group: {
  _id: "$customerID",
  totalAmount: { $sum: "$amount" },
  totalpaidAmount: { $sum: "$paidAmount" }
 },
 $addFields:{
   totalAmountDue: { $subtract: ["$totalAmount", "$totalpaidAmount"] }
 }
}];

关于mongodb - MongoError:$ subtract累加器是一元运算符,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50088576/

10-10 00:35