I was reading the API documentation (and was experimenting a bit), but it seems that cursor.count()
no longer exists, so I was wondering if it was possible to get a count of your aggregate. This is because I would like to know how many documents there are in total, while still limiting and skipping results. I'm currently using $facet to facilitate this, but unsure if there's a better method.
{
$facet:
{
"results":
[
{
"$skip":
start
},
{
"$limit":
finish
},
],
"total":
[
{
"$count":
"total"
},
]
}
}
Using $facet
for this use case is fine, both for style and performance. It should be noted, though, that this is essentially two queries in one, albeit server-side optimized. Getting the full count of the filtered material (or no filter at all if no initial $match
) takes time regardless of the $skip/$limit
setup. If the $match
produces a relatively small amount of material, then $count
will be fast. The dynamics involved are very similar to those in the regular SQL world, eg Run a query with a LIMIT/OFFSET and also get the total number of rows
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.