谁能告诉我为什么要通过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);
});
});

最佳答案

首先需要像这样的猫鼬:

var Mongoose = require('mongoose');


然后使用:

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


代替:

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

09-07 13:18