Simple Pagination with Node.js, Express, and Mongoose

Kevin Luu
Kevin Luu
Mar 14, 2020 · 3 min read
Image for post
Image for post
Photo by Alexander Sinn on Unsplash

Keeping it simple with Pagination

Are you looking to limit the amount of the documents that comes back from MongoDB per request? We’ve all seen it before, pagination to the rescue! There are many ways to implement pagination but I will show you the simplest approach (in my opinion) to get it done.

This approach allows the client to supply two query parameters in the request: and. Page refers to the current page you are requesting. Limit is the amount of documents you wish to retrieve.

Let’s Get Started

Now, I am going to assume that you have basic knowledge working with Node.js, Express, and Mongoose. Below, we have a simple express server running on port 3000 with a mongoose connection. We also import a mongoose model so we can use it in the route handler.

In the code above, when we make a GET request to “/posts”, we will retrieve all the documents in the collection. Let’s modify the route to incorporate pagination.

Let’s break it down. When a request is made to the “/post” route, we are destructing and from the request query. We are also providing default values for them if they are not provided. If they are provided, they will override the default values.

Now, let’s look at the mongoose query. Limit is the number of documents we want to retrieve. Skip is the amount of documents we want to skip before retrieving our documents. For skip, we take the and subtract one,then multiply it by the . On line 10, we grab the total documents and store it in a variable so we can calculate total pages by dividing the count by the limit. Lastly, we return the posts, total pages, and current page. We wrap everything in a try/catch to catch any exceptions.

Testing our Simple Pagination Solution

To test our simple pagination solution, let’s use Postman to make a GET request to the “/posts” with query parameters of set to 1 and set to 2.

Image for post
Image for post

Viola! There you have it! Instead of retrieving all the documents, we are now retrieving page 1 of 50 with limit of 2.

Conclusion

I want to reemphasize that there are more than a few ways to implement pagination. If you are new to working with pagination, I found this to be the simplest solution. In the near future, I will cover cursor pagination, which provides a unique identify for a specific record and is more performant when working with very large data sets. I hope you found this article helpful and you are able to implement this into whatever project you may be working on.

I write these articles for fun during my spare time. If you have enjoyed this article, please leave your likes and comments below! You can follow me on Medium and Twitter. Thanks for the support!

JavaScript In Plain English

New JavaScript + Web Development articles every day.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store