Ohjay44 Ohjay44 - 2 months ago 19
Javascript Question

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?

Answer

It's possible to do so in a single query.

You can project the filtered array using $project into two different fields: one with the content and one with the count.

You can use $slice to limit the content array.

 db.collection.aggregate([
    {
        $match: {} // Filter
    },
    {
        $group: {
            _id: 1,
            array: {$push: '$$ROOT'}
        }
    },
    {
        $project: {
            content: {
                $slice: ['$array', skip, limit]
            },
            total: {
                $size: '$array'
            }
        }
    }
], {allowDiskUse: true})