Get a count of total documents with MongoDB when using limit

Mongodb 3.4 has introduced $facet aggregation which processes multiple aggregation pipelines within a single stage on the same set of input documents. Using $facet and $group you can find documents with $limit and can get total count. You can use below aggregation in mongodb 3.4 db.collection.aggregate([ { “$facet”: { “totalData”: [ { “$match”: { }}, … Read more