'How to fetch count and resultsets using mongodb $match and $group

How to get the resultsets on matching $match along with count . I am trying to get say initial 5 records with complete resultsets along with total number of matched records.

Here is my trial code:

let querySpec = [{
        $match: {
            entity: "project",
            roles: {
                $in: roles
            },
            deleteFlag: {
                $exists: false
            }
        }
    },
    {
        $group: {
            _id: null,
            count: { $sum: 1 }
        }
    },
    { $limit: event.rows },//5
    { $skip: event.first }//0

    ];

expected:

{[data:array[5],count:10]}

but getting :
{_id: null, count: 10}

tried using facets but it fails to fetch next result sets:

var querySpec = [
    {
        "$facet": {
            "data": [
                {
                    $match: {
                        entity: "project",
                        $or: [{
                            accessType: "Private",
                            userName: userName
                        }, {
                            accessType: "Public"
                        }],
                    }
                },

                { $limit: event.rows },
                { $skip: event.first },
                { $sort: { [event.sortField]: event.sortOrder } }
            ],
            "totalRecords": [
                {
                    $match: {
                        entity: "project",
                        $or: [{
                            accessType: "Private",
                            userName: userName
                        }, {
                            accessType: "Public"
                        }],
                    }
                },
                { "$count": "total" }
            ]
        }
    }

];

output: {data: Array(5), totalRecords: Array(1)} but for next pagination click it fails to fetch recordsets {data: Array(0), totalRecords: Array(1)}:



Solution 1:[1]

let querySpec = [
    {
        $match: {
            entity: "project",
            roles: {
                $in: roles
            },
            deleteFlag: {
                $exists: false
            }
        }
    }, {
        $facet: {
            'data': [ // the key in which you want your data
                // add sort here if needed
                {
                    '$skip': event.first
                }, {
                    '$limit': event.rows
                }
            ],
            'count': [
                {
                    '$count': 'count'
                }
            ]
        }
    }, {
        '$addFields': {
            'count': { // the key in which you want the count
                '$arrayElemAt': [
                    '$count.count', 0
                ]
            }
        }
    }]

try this it will remove the data which does not matched your query. Also skip should be used before limit.

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Smriti Shikha