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:
- Sorting will happen based on a column. Its values should be unique and then only one can observe sorting is done efficiently.
- Otherwise along with the specified column, always try to combine _id column as well as _id column should contain unique values only.
- 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.
Similar Reads
What is an API Endpoint ?
The API endpoint is the specific URL where requests are sent to interact with the API. In this article, we will discuss API Endpoint their working and the differences between REST API and GraphQL endpoints. Table of Content What is an API Endpoint?How do API endpoints work?What are some best practic
7 min read
How to test an API using Postman ?
API testing is a software testing type that tends to validate the application programming interfaces. As per Postman API, API testing is confirming that an API is working as expected. It sends requests to an API and monitors the responses to check its behavior to assess the functionality, reliabilit
5 min read
FastAPI - OpenAPI
FastAPI is a modern, fast, and web framework for creating APIs with Python. It is built on top of the popular web framework Starlette and includes built-in support for OpenAPI. In this article, we'll learn about OpenAPI and FastAPI and how to use them to create robust, widely documented APIs. What i
3 min read
Swagger and API Monitoring
API monitoring is like having a watchful guardian for your digital communication paths, playing a big role in making software work well. It keeps an eye on how things are going, manages mistakes, makes sure everything responds quickly, checks everything in real-time, makes the user experience better
6 min read
Return an Image in FastAPI
FastAPI is a Python web framework that makes it easy to build APIs quickly and efficiently. Returning an image is a common requirement in web development, and FastAPI makes it straightforward to do. another famous framework for doing the same is Flask, which is also Python-based. In this article, we
4 min read
What is an API call?
The full form of the API is Application programming interface Basically an API call is request by a software application to access data or any other service from another application or any other server. API calls are essential for enabling communication and data exchange between different software s
6 min read
FastAPI - Pydantic
In this article, we will discuss FastAPI and Pydantic by using an example. So, let's get started. FastAPI and Pydantic are two potent tools within the Python ecosystem, highly acclaimed for their roles in crafting resilient and efficient web APIs. This article will guide you through the process of e
7 min read
FastAPI - Crud Operations
We will explore how to implement CRUD operations with FastAPI. CRUD operations are essential in any web application, including creating new records, retrieving existing records, updating existing records, and deleting records from a database. What is CRUD in FastAPI?CRUD refers to the basic operatio
5 min read
FastAPI - Pydantic
Before we dive into Pydantic, let's briefly introduce FastAPI. FastAPI is a modern Python web framework that simplifies the process of creating APIs. Its rapid adoption within the Python community is attributed to its exceptional performance and developer-friendly features. With FastAPI, developers
5 min read
FastAPI - Introduction
Developers are continuously on the lookout for technologies that allow them to rapidly and efficiently construct sophisticated APIs and online applications. FastAPI, a relatively new addition to the Python web framework landscape, has quickly garnered traction due to its speed, simplicity, and devel
5 min read