I am interested in optimizing a "pagination" solution I'm working on with MongoDB. My problem is straight forward. I usually limit the number of documents returned using the limit()
functionality. This forces me to issue a redundant query without the limit()
function in order for me to also capture the total number of documents in the query so I can pass to that to the client letting them know they'll have to issue an additional request(s) to retrieve the rest of the documents.
Is there a way to condense this into 1 query? Get the total number of documents but at the same time only retrieve a subset using limit()
? Is there a different way to think about this problem than I am approaching it?
Times have changed, and I believe you can achieve what the OP is asking by using aggregation with
$sort
,$group
and$project
. For my system, I needed to also grab some user info from myusers
collection. Hopefully this can answer any questions around that as well. Below is an aggregation pipe. The last three objects (sort, group and project) are what handle getting the total count, then providing pagination capabilities.Try as bellow:
there is a way in Mongodb 3.4: $facet
you can do
then you will be able to run two aggregate at the same time
MongoDB allows you to use
cursor.count()
even when you passlimit()
orskip()
.Lets say you have a
db.collection
with 10 items.You can do:
It all depends on the pagination experience you need as to whether or not you need to do two queries.
Do you need to list every single page or even a range of pages? Does anyone even go to page 1051 - conceptually what does that actually mean?
Theres been lots of UX on patterns of pagination - Avoid the pains of pagination covers various types of pagination and their scenarios and many don't need a count query to know if theres a next page. For example if you display 10 items on a page and you limit to 13 - you'll know if theres another page..
It is possible to get the total result size without the effect of
limit()
usingcount()
as answered here: Limiting results in MongoDB but still getting the full count?According to the documentation you can even control whether limit/pagination is taken into account when calling
count()
: https://docs.mongodb.com/manual/reference/method/cursor.count/#cursor.countEdit: in contrast to what is written elsewhere - the docs clearly state that "The operation does not perform the query but instead counts the results that would be returned by the query". Which - from my understanding - means that only one query is executed.
Example: