MongoDB

How to use MongoDB Pagination

MongoDB is a widely used NoSQL database that practices managing data in JSON-type documents and these documents are contained in a collection. Sometimes, it becomes necessary to retrieve only a few documents or you may look for a clean output. In MongoDB, the pagination phenomenon is used to get an output that can be easily understandable. Pagination is a procedure to display large disarranged output in a page format. With the help of pagination, the result can be retrieved faster as compared to the general methods of MongoDB.

The pagination contains several methods and operators that are focused to give you a better output. In this article, we have demonstrated the pagination concept in MongoDB by explaining the maximum possible methods/operators that are used for pagination.

How to use MongoDB pagination

MongoDB supports the following methods that can work for pagination. In this section, we will explain the methods and operators that can be used to get an output that looks good.

Note: In this guide, we have used two collections; they are named as “Authors” and “staff“. The content inside “Authors” collection is shown below:

> db.Authors.find().pretty()

And the second database contains the following documents:

> db.staff.find().pretty()

Using limit() method

The limit method in MongoDB displays the limited number of documents. The number of documents is specified as a numeric value and when the query reaches the specified limit, it will print the result. The following syntax can be followed to apply the limit method in MongoDB.

> db.collection-name.find().limit()

The collection-name in the syntax must be replaced with the name on which you want to apply this method. Whereas the find() method shows all the documents and to limit the number of documents, limit() method is used.

For instance, the below mentioned command will print only first three documents from “Authors” collection:

> db.Authors.find().limit(3).pretty()

Using limit() with skip() method

The limit method can be used with the skip() method to fall under the pagination phenomenon of MongoDB. As stated, the earlier limit method displays the limited number of documents from a collection. Contrary to this, the skip() method is helpful to ignore the number of documents that are specified in a collection. And when limit() and skip() methods are used, the output is more refined. The syntax to use limit()and skip() method is written below:

db.Collection-name.find().skip().limit()

Where, skip() and limit() only accept numeric values.

The command mentioned below will perform the following actions:

  • skip(2): This method will skip the first two documents from the “Authors” collection
  • limit(3): After skipping the first two documents, the next three documents will be printed
> db.Authors.find().skip(2).limit(3)

Using Range Queries

As the name shows, this query processes the documents based on range of any field. The syntax to use range queries is defined below:

> db.collection-name.find().min({_id: }).max({_id: })

The following example shows the documents that fall between the range “3” to “5” in “Authors” collection. It is observed that the output starts from value(3) of min() method and ends before value(5) of max() method:

> db.Authors.find().min({_id: 3}).max({_id: 5})

Using sort() method

The sort() method is used to rearrange the documents in a collection. The arrangement order can be either ascending or descending. To apply sort method, the syntax is provided below:

db.collection-name.find().sort({<field-name>: <1 or -1>})

The field-name can be any field to arrange documents on the basis of that field and you can insert “1′ for ascending and “-1” for descending order arrangements.

The command used here will sort the documents of the “Authors” collection, with respect to the “_id” field in descending order.

> db.Authors.find().sort({id: -1})

Using $slice operator

The slice operator is used in the find method to cut short the few elements from a single field of all documents and then it will display only those documents.

> db.collection-name.find({<field-name>, {$slice: [<num>, <num>]}})

For this operator, we have created another collection named “staff” that contains an array field. The following command will print the number of 2 values from the “random” field of the “staff” collection using the $slice operator of MongoDB.

In the below-mentioned command “1” will skip the first value of the random field and “2” will show the next “2” values after skipping.

> db.staff.find({},{random: {$slice: [1,2]}})

Using createIndex() method

The index plays a key role to retrieve the documents with minimum execution time. When an index is created on a field then the query identifies the fields using the index number instead of roaming around the whole collection. The syntax to create an index is provided here:

db.collection-name.createIndex({<field-name>: <1 or -1>})

The <field-name> can be any field, whereas the order value(s) is constant. The command here will create an index on the “name” field of the “Authors” collection in ascending order.

> db.Authors.createIndex({name: 1})

You can also check the available indexes by the following command:

> db.Authors.getIndexes()

Conclusion

MongoDB is well known for its distinctive support to store and retrieve documents. The pagination in MongoDB helps Database Administrators to retrieve documents in an understandable and presentable form. In this guide, you have learned how the pagination phenomenon works in MongoDB. For this, MongoDB provides several methods and operators that are explained here with examples. Each method has its own way to fetch documents from a collection of a database. You may follow any of these that best fit your situation.

About the author

Adnan Shabbir