得到null,$ sum aggregate - Mongo

时间:2018-04-12 15:09:41

标签: json node.js mongodb mongoose aggregation-framework

任何人都可以告诉我为什么在尝试通过vendorID汇总广告系列中的预算值时会收到[]?

在数据库中显示我的纪录 - Campaign Collection

{
"_id" : ObjectId("5acf6ca923cdca5be18518cf"),
"vendorID" : ObjectId("5acf11eaeab7b52d8403ac17"),
"bookingID" : ObjectId("5acf6c9123cdca5be18518ce"),
"listingID" : ObjectId("5acf6c4d23cdca5be18518cd"),
"budget" : 200000,
"date" : "Thu Apr 12 2018 17:26:49 GMT+0300 (EAT)",
"__v" : 0
}

这是路由器块我使用

router.get('/campaign_sum/:vendorID', (req, res, next) => {
Campaign.aggregate([
    { $match : { "vendorID" : req.params.vendorID } },
    { 
        $group: {
            _id: null,
            total: { $sum: "$budget" }
        }
    }
], (err, value) => {
    res.json(value);
});
});

1 个答案:

答案 0 :(得分:1)

首先需要像这样的猫鼬:

var Mongoose = require('mongoose');

然后使用:

{ $match : { "vendorID" :  Mongoose.Types.ObjectId(req.params.vendorID)} }

代替:

{ $match : { "vendorID" : req.params.vendorID } }