I can't seem to find any solutions for this problem. I just can't figure out what is wrong.
I already tried this Mongo subtract in group aggregation but no hope. The post also failed to explain further.
Here is my code:
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,
});
});
it prompts MongoError: The $subtract accumulator is a unary operator everytime I query it.
Change your query part to this one:
aggregateOptions = [
{
$match: {
}
},
{
$group: {
_id: "$customerID",
totalAmount: { $sum: "$amount" },
totalpaidAmount: { $sum: "$paidAmount" }
}
},
{
$addFields:{
totalAmountDue: { $subtract: ["$totalAmount", "$totalpaidAmount"] }
}
}
];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With