如何从聚合查找结果中获取计数

时间:2016-03-26 04:10:46

标签: mongodb mongodb-query

我希望找出聚合加入的总文件数。是否有可能从下面的代码计数?如果有人有想法请告诉我。

    db.collection("camp", function (err, camp) {
    camp.aggregate([{$match: {status: {$ne: "Rejected"}}}, 
         {'$lookup': {

                from: "organization",
                localField: "organization",
                foreignField: "organizationId",
                as: "orgnData"
            }
        }, {'$lookup': {
                from: "organizer",
                localField: "organizer",
                foreignField: "organizerId",
                as: "ornrData"
            }},
        {$unwind: "$orgnData"},
        {$unwind: "$ornrData"},
        {$match: {'ornrData.city': 'Nagpur'}},

        {$skip: 0}, {$limit: 10}, 
        {$sort: {id: -1}}

    ], function (err, info) {
        res.json(info);
    });
});

从上面的代码我希望得到限制之前的结果的总行数。如果有人有想法请告诉我?

1 个答案:

答案 0 :(得分:-2)

将以下内容添加到聚合中,删除限制:

{ $group: { _id: null, count: { $sum: 1 } } }