mongoDB 中的条件 $sum 查询

编码器_B

我想在特定用户的 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章