'MongoDB Node Driver Count of current aggregation

I am using mongodb for node and am trying to aggregate a collection of documents based on some set filters and then limit it to 10. I have it aggregating just fine and limiting just fine but I need to get the total number of that aggregated documents before I limit them to 10.

Here is my code.

var qry = [];
if (filter.FocusArea && filter.FocusArea != "(None)") {
    qry.push({
        $match: { 'ProgramAreaId': filter.FocusArea }
    });
}
if (filter.Status && filter.Status != "(None)") {
    qry.push({
        $match: { 'StatusId': filter.Status }
    });
}
if (filter.ProgOfficer && filter.ProgOfficer != "(None)") {
    qry.push({
        $match: { 'ProgramOfficerId': filter.ProgOfficer }
    });
}
if (filter.Fund && filter.Fund != "(None)") {
    qry.push({
        $match: { 'FundId': filter.Fund }
    });
}

var skipNbr = (parseInt(filter.Page) * 10 - 10);
qry.push({ $project: { _id: '$_id', count: { $sum: '$$ROOT' }, content: '$$ROOT'} }) // I would like to do the count here.
qry.push({ $skip: skipNbr })
qry.push({ $limit: 10 })
var apps = mongo.collection('Applications').aggregate(qry, function(err, docs) {
    callback(docs);
});

Can this be done in one aggregation query or does it need to be split into two?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source