Server Side Pagination Using Node and Mongo

In a real-world scenario, most of the time we have to deal with a huge number of data. So imagine a scenario where you have 1 million records in the database and your requirement is to show it on a web page using an API.

You just can’t pick up all records and show it, it’s just not possible and feasible. To handle such cases, we use pagination i.e showing record page by page, just like the book.

There are two ways you can implement pagination.

Client Side Pagination

Server Side Pagination

Client side pagination can work if you are sure that record set is static i.e not growing day by day which is I believe very rare nowadays. In this approach, we load the data in the browser and paginate it. It is fast and easy but not reliable to hold large volume (Because of browser crash you know).

For a large volume of data, we use Server side pagination.

Today, we are going to learn Server Side pagination and we are storing data in MongoDB and paginating it using API’s built-in Nodejs.

Let’s create our Server code. In MongoDB find query, there is a parameter to skip the record and limit it without doing a manually filtering i.e MongoDB itself looks for the records based on this for fast performance.

We will skip the records based on the page number and limit it based on the size. So the page size will be the number of records we want i.e limit in MongoDB term and a number of records to skip would be page size multiplied by page number.

Example:

User request for page number 1 with size 10

So, in the query, we will skip no records and get the first 10 records because of its page number one.

Running the app

Further development

We need to also extract the total number of pages based on the size provided by the user. So for example, if user wants 10 records a time for 1000 records in total, then we need to tell the API consumer that you can traverse complete record in 10 pages.

We can do this by getting the count of records and then dividing it by the size asked by the user.

Further Study

Conclusion

We have learned how to do the pagination using MongoDB, you can, however, replace it with any other database. I highly recommend you to perform the Server side pagination as it is easy to scale and fix.

I think i am getting problem in running the application.
Kindly tell where and how to enter the command “node app.js”
should i first navigate to the folder where i saved both node.js and app.js and there i should run this command or in Node.js shell?

I am trying to implement this at my end but it is not working.
I am not getting any console error i did some debugging and found in app.js file mongoOp.find is not getting called.
I have tried everthing but im not able to find the root cause. need your help on this.
my mongodb version is 2.4.6.

Hello, the works perfectly fine. But I am a bit confused why we pass in another empty object to the User.find() method. when I don’t pass in the second object I just get a bunch of all the users id in my database. Can you help me clarify this because I just the code to work, I want to know why it works the way it does. Thanks