Pagination in MongoDB Aggregation and Find Queries with Total Count, Skip, Limit with Examples

Partha Roy
Fasal Engineering
Published in
4 min readOct 6, 2022

I have been using MongoDB as the primary database platform for the last 3 years and the experience has been amazing. Last month I had a session with my students on MongoDB, and that’s when I understood there are many concepts in MongoDB which are pretty common strategies in the industry but quite underrated. So I will be talking about those concepts in detail in my future articles.

In this article, I will be talking about different pagination mechanisms in MongoDB queries.

Also thank you so much for showering so much love on my last piece on MongoDB:

✅ Also checkout, Top 20+ Must-Have Modern Desk Accessories to get in India

What is pagination? Why it’s required?

MongoDB is notoriously famous for being able to handle large sets of data. However, our server or client restricts us to pull off a lot of data at once for performance issues or client-side network limitations. Therefore, getting a lot of data at once is never the right way. Rather, we should always bring data in chunks as and when necessary.

This technique is called Pagination where we get data in chunks of a particular size (offset) defined by pages.

Pagination is very important because,

  • It ensures there’s no unnecessary load on the database.
  • It guarantees that no over-fetching of data is happening.
  • Client application remains well optimized and faster due to paginated data queries.

How to perform pagination in a find query

To perform pagination in a find() query (where it enables us to select documents in one collection or view and returns a cursor to the selected documents), we can use the cursor.skip() and the cursor.limit() method along with the query.

Underlying tech

The cursor.skip() method controls from which point MongoDB begins returning results. This method expects one parameter called offset, which tells how after how many documents MongoDB should begin to fetch or simply put how many documents must be skipped.

On the other side the cursor.limit() method specifies the maximum number of documents that should be fetched on every run.

Combining the above two methods, we can tell the MongoDB engine how many documents to fetch and how many to skip, which can be simplified to:

cursor.skip(current_page_number * number_of_documents_each_page).limit(number_of_documents_each_page)

Note: Just like the array index in javascript, in MongoDB, the page number should be thought of starting from 0 while considering the above formula.

MongoDB Pagination Explained

Code Example

const no_of_docs_each_page = 2; // 2 docs in single page
const current_page_number = 2; // 3rd page
db.collection.find({})
.skip(no_of_docs_each_page * current_page_number)
.limit(no_of_docs_each_page)

How to do pagination in an aggregate query

Just like the find query, in aggregation queries also there’s a similar skip & limit pattern. The difference is here both skip and limit are two different stages in the aggregation pipelines unlike the cursor methods in find queries.

Underlying tech

The $skip stage skips over the specified number of documents that pass into the stage and passes the remaining documents to the next stage in the pipeline. — MongoDB Documentation

The $skip stage has the following prototype form:

{ $skip: <positive 64-bit integer> } 

We have to use another stage, $limit which specifies how many documents should be passed to the next stage.

Code Example

const no_of_docs_each_page = 2; // 2 docs in single page
const current_page_number = 2; // 3rd page
db.collection.aggregate([
{ $skip : no_of_docs_each_page * current_page_number },
{ $limit : no_of_docs_each_page }
]);

Also apart from this, there’s a very interesting pagination mechanism called Bucket Pattern, you can check that out here —

How to get the total number of documents in a MongoDB collection

Sometimes getting the count of total documents existing for a particular condition is very necessary. Here we will learn about how we can do that.

In Find Query

By using cursor.count() method we can get the number of matching documents in any find query. E.g.

db.collection.find({}).count()

In Aggregation Query

To count the number of documents in the aggregation query we can use $count stage which has the following format,

{ $count: <string> }

where the string represents the name of the value ( count ).

db.collection.aggregate( [ { $count: 'document_count' } ] )

That was all about Pagination in MongoDB. Please let me know in the comments if you have any questions about this, I will be happy to answer those.

Ciao.

--

--

Partha Roy
Fasal Engineering

Full-Stack Engineer 👨🏻‍💻 | ReactJs Dev ⚙️ | Tech Mentor 👨🏻‍🏫