The concept of MongoDB pagination is interesting and complex. In this resource, we're going to take a look at several ways you can add pagination to your application using MongoDB as a database solution.
MongoDB is a document based data storage solution, so paging data is a common requirement. Pagination is also a resource intensive process, so doing at the database level is always a good idea.
There are a few methods that we can use to implement pagination in MongoDB (skip to solutions).
Pagination is a universal concept in UI/UX. By definition, pagination breaks large datasets into smaller, more manageable chunks that users can easily navigate through.
Traditionally, they're paired with queries/filters/sorting.
Let's say I've built a blog / content website with 100s of old posts, and new ones being added consistently.
I would want to show the 10 most recent published posts, and allow users to navigate through "chunks" of older posts.
I would use pagination with MongoDB to do just that.
Skip + Limit
This is the easiest way to implement pagination, but typically not the best or most efficient.
We will use:
First, the skip(n), where n equals the number of documents to skip over, essentially jumps over content. Then, the limit (n), where n equals the number of documents returned by the cursor, only shows a "chunk" of content.
By combining both, we can return 10 documents, and when moving to the next "page", we can skip over 10.
In the shell, here's what our commands would look like:
Easy pagination in MongoDB.
But, while this method works in a pinch, it's not the best option for large datasets.
That's because it scans the entire dataset from start to finish. Also, if a new document is added, it will mess up the pagination - this is an important, but overlooked consideration for more constantly updated applications.
range query (+ limit)
This method is actually recommended in the MongoDB documentation. Here's a direct quote:
Range queries can use indexes to avoid scanning unwanted documents, typically yielding better performance as the offset grows compared to using
skip()
for pagination.
This is a three step process. First choose a field with a UID, and one that changes consistently over time (for example, the _ID field which increases with every new doc addition).
Then, query for docs who has a field less than the start value. Mongo suggests using the _id field and implementing the $lt
and sort()
operators.
As a note, $lt will select documents where the value of _id is less than the specified value. Perfect for pagination and efficient too!
Finally, store the last-seen field value for the next query.
Here's example code from the MongoDB docs (as a js function):
This is a better method as it's more efficient and extendable. However it's a bit more complex.
And there are two ways that you can easily add pagination to MongoDB.
Hey, very good and concise post, love it!
just one thing - in your first code example fetching the 3rd page should look like
but you skip 10