【发布时间】:2018-10-09 20:15:45
【问题描述】:
我似乎找不到任何解决此问题的方法。我就是不知道哪里出了问题。
我已经尝试过Mongo subtract in group aggregation,但没有希望。该帖子也未能进一步解释。
这是我的代码:
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: The $subtract accumulator is a unary operator。
【问题讨论】:
-
算术运算符不能在
group阶段使用。相反,有一个额外的project阶段或addFields阶段,通过subtract操作添加新字段。