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:
And the second database contains the following documents:
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.
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:
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:
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
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:
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:
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:
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.
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.
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.
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:
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.
You can also check the available indexes by the following command:
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.