Open In App

Pagination on an API

Last Updated : 27 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

MongoDB is an efficient NoSQL database. It is used across the software industry to a great extent as it is open source, easily configurable, maintenance wise it is easier, upgradation wise, portability wise it plays a major role and hence it is a more like NoSQL database.

In this tutorial, let us see how to do Pagination in MongoDB query. For that, let us assume that MongoDB (Compass) or earlier versions of MongoDB is installed.

The current database is geeksforgeeks and it is having a collection name called “authordetails”.

Let us query them first:

use geeksforgeeks
db.authordetails.find().pretty()

Corresponding details are shown in the below image:

 

Assume that there are a lot of documents present in a collection. Then it will be challenging to display them in the Mongo Shell or if we pass the query without pagination, it is harder to display the results in the front end. Hence we require a mechanism to limit the documents.

Before moving to see the limit(), let us see how many documents are available. It can be easily retrieved by means of

 

1. limit():

This method fixes a limit on a cursor (resultant MongoDB document output). In the SQL database, there is a LIMIT statement. Similarly, the limit() method helps to limit the document output. 

It has to be given before retrieving any documents from the collection.

It helps to maximize performance. i.e. If we need only 5 documents, we can limit that easily with this method.

usage of limit(): Initially, there were 5 documents for our author details collection (But in a real example or the REST API output that got stored in MongoDB should definitely have more documents).

By using the below query, we can able to retrieve only 2 documents

db.authordetails.find().pretty().limit(2)

NOTE: In the place of 2 we can specify how many documents are to be retrieved.

 

Limitation: limit(value) where the value should be between -2^31 and 2^31. If  other values are given, it is ignored

Providing a negative value like -2 or so will produce the same result as a positive value. The difference between a positive value and a negative value with the limit() method is, that with a negative value, the cursor is closed after returning a single batch of results.

By setting limit(0), there is no effect with limit(), i.e. no effect is observed when limit(0) is given.

With limit() and sort(): We need to add at least one field in sort() that contains unique values and this has to be done before passing it to the limit() method.

Try to perform sorting on unique values, otherwise, duplicate values produce inconsistent sort order.

  • Ascending order sort and limit combination:

db.authordetails.find().sort( { “authorName” : 1}).limit(2).pretty()

 

  • Descending order sort and limit combination:

db.authordetails.find().sort( { “authorName” : -1}).limit(2).pretty()

 

During report visualization in front-end tools like ReactJS and AngularJS, sort and limit combination will provide efficient results.

So for Pagination, limit() is highly used and along with sort(), it is having more advantages.

2. skip():

Sometimes there are situations to skip certain documents and this is purely based on requirements. When skipping (it will have a positive 64-bit integer value) as an argument, the process skips over the specified number of documents, and the rest of the documents are available for the next stage in the pipeline. It is coming under aggregations

Limitation: $skip takes a positive integer. Starting from MongoDB 5.0, $skip has a 64-bit integer limit. Exceeding this limit will result in an invalid argument error.

Let us see the behavioral result of the skip below:

db.authordetails.aggregate([{$skip : 2}]).pretty()

 

skip and sort() combination: It will be good practice to show the output of paginated results in the frontend tools like ReactJS and AngularJS.

The time below kind of queries are much helpful:

  • Ascending order sorted paginated results by using skip() aggregation

db.authordetails.aggregate ([ {$sort : { “authorName” : 1}}]).skip(3).pretty()

 

  • Descending order sorted paginated results by using skip() aggregation:

db.authordetails.aggregate ([ {$sort : { “authorName” : -1}}]).skip(3).pretty()

 

So by using skip (a positive value), we are achieving pagination. Sorting and combining either with limit()/skip() are a more advantageous way of pagination.

Important points to note for sorting:

  1. Sorting will happen based on a column. Its values should be unique and then only one can observe sorting is done efficiently.
  2. Otherwise along with the specified column, always try to combine _id column as well as _id column should contain unique values only.
  3. Among the available columns, a column can be provided with ascending order way as well as a different column can be provided in the descending order way.

Hence with sort() mechanisms, both limit() and skip() provide the paginated results in a nicer manner.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads