我想在特定用户的 mongodb 数据库中获取已归档的 reqAmount 总和。
这是架构
const userid = req.body.userId;
const id = mongoose.Types.ObjectId(userid);
fundReq.aggregate([
{ $match : { userId : id } },
{
$group: {
_id : '',
total: {$sum : "$reqAmount"}
}
}
],function (err, result) {
if (err) throw err;
else res.json(result);
})
但结果为空...
这对你来说很好用
const ObjectId = require('mongodb').ObjectId;
function fnGetTotalCollectionAmount(callback) {
TransactionModel.aggregate([
{
$match: { '_id': ObjectId(productId) }
},
{
$group: {
_id: null,
grandTotal: {
$sum: '$subTotal'
}
}
}
]).exec(function (err, transaction) {
if (err) {
return callback(err);
} else {
return callback(transaction[0].grandTotal);
}
});
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句