How to get count from aggregate lookup results [duplicate]

岁酱吖の 提交于 2019-12-12 03:33:53

问题


I am looking to find out the total documents count from aggregate join. is it possible to get count from the below code? if anybody have an idea please let me know.

    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);
    });
});

from above code i am looking to get total rows count of result before limit. if anybody have an idea please let me know?


回答1:


Add to the aggregate the following, removing limit:

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


来源:https://stackoverflow.com/questions/36231528/how-to-get-count-from-aggregate-lookup-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!