我似乎找不到这个问题的任何解决方案。我只是不知道怎么了。
我已经在群聚中尝试过此Mongo减法,但没有希望。该帖子也无法进一步解释。
这是我的代码:
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,
});
});
Run Code Online (Sandbox Code Playgroud)
提示MongoError:每次我查询$ subtract累加器都是一元运算符。
Rah*_*Raj 10
将查询部分更改为此:
aggregateOptions = [{
$match: {
}
},{
$group: {
_id: "$customerID",
totalAmount: { $sum: "$amount" },
totalpaidAmount: { $sum: "$paidAmount" }
},
$addFields:{
totalAmountDue: { $subtract: ["$totalAmount", "$totalpaidAmount"] }
}
}];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4266 次 |
| 最近记录: |