For a big collection of documents, it is faster to execute 2 queries (the paginated results and the total count) or it is faster to use aggregations?
Example aggregation query
[
{ $match: conditions },
{ $sort: { createdAt: -1 } },
{
$group: {
_id: null,
count: { $sum: 1 },
results: { $push: "$$CURRENT" },
},
},
{
$project: {
count: 1,
results: { $slice: ["$results", 0, 10] },
},
},
]
I would appreciate it if you could give me an example