How to Paginate with Mongoose in Node.js?
Last Updated :
12 Mar, 2024
When working with large datasets in Node.js applications using Mongoose, fetching and displaying all records at once can lead to performance issues. Pagination is a technique that allows us to break down data into smaller, more manageable manner. In this article, we'll explore how to implement pagination with Mongoose in Node.js.
How to Paginate with Mongoose in Node.js?
When dealing with large datasets, fetching and displaying all records at once can be inefficient and lead to performance issues. Pagination allows us to break down the data into smaller, manageable manner. Below are the traditional ways to paginate with Mongoose that help us to perform pagination with Mongoose in Node.js are as follows:
- Offset-based pagination
- Cursor-based pagination
1. Offset Based Pagination
Offset-based pagination involves specifying a fixed number of items to skip (offset) and a limit on the number of items to retrieve per page.
For example, if we want to display 10 items per page, we would skip the first "(page number - 1) * 10" items and fetch the next 10. This method is simple and easy to implement but can become inefficient as the offset increases, especially with large datasets. The main drawback is that skipping a large number of items on each request can impact performance.
Syntax:
const data = await Model.find().skip(offset).limit(limit);
Example: The below code uses offset-based pagination to paginate the data with Mongoose in Node.js.
// Define the limit and page number for pagination
const limit = 5;
const page = 1;
// Calculate the offset based on the page number and limit
const offset = (page - 1) * limit;
// Retrieve notes from the database with pagination
const notes = await Note.find()
.skip(offset)
.limit(limit);
// Prepare the response object with pagination metadata and notes data
const response = {
status: 200,
totalResults: notes.length, // Total number of notes in the current page
notes: notes.map(note => ({ // Map each note to a simplified object
_id: note._id,
user: note.user,
title: note.title,
desc: note.desc,
category: note.category,
isCompleted: note.isCompleted,
createdAt: note.createdAt,
updatedAt: note.updatedAt,
_v: note._v
}))
};
// Output the response
console.log(response);
Output:
Offset Based Pagination
Explanation:
- This Node.js code defines a function "getNotes" for fetching and paginating notes using the Mongoose model Notes.
- It starts by importing the necessary requirements, including the Mongoose model for tasks. The function initializes parameters for pagination, such as page, limit, and offset.
- It then performs pagination using the Mongoose find method with the skip and limit functions, retrieving a subset of notes based on the specified page and limit.
- The paginated data, along with metadata such as the total number of results, is returned in a JSON response with a status code of 200.
2. Cursor-based pagination
- Cursor-based pagination is a more efficient approach, especially for large datasets.
- Instead of depend on an offset, it uses a cursor, which is a unique identifier pointing to a specific record.
- The server sends the cursor along with each page of results, and the client uses it to request the next set of records. Cursors can be based on a sortable field (like a timestamp or unique ID) and are more resilient to changes in the dataset.
- This method is well-suited for real-time applications and overcome the drawback of offset-based pagination as it scales.
Syntax:
let query = {}; // write any query
query._id = { $gt: cursor };
Example: The below code uses cursor-based pagination to paginate the data with mongoose in Node.js.
// importing all requirements
const Notes = require('../models/Task');
// to fetch the notes and paginate the data
const getNotes = async (req, res) => {
const { cursor, limit = 10 } = req.query;
let query = {};
// If a cursor is provided, add it to the query
if (cursor) {
query._id = { $gt: cursor };
}
// Fetch notes using the cursor-based query
const notes = await Notes.find(query).limit(Number(limit));
// Extract the next and previous cursor from the result
const prevCursor = cursor && notes.length > 0 ? notes[0]._id : null;
const nextCursor = notes.length > 0 ? notes[notes.length - 1]._id : null;
// Return the paginated data
return res.status(200).json({
status: 200,
nextCursor,
prevCursor,
totalResults: notes.length,
notes,
});
}
// exporting notess functions
module.exports = { getNotes };
Output:
Cursor-based pagination
Explanation:
status
: The HTTP status code indicating the success of the request (200 for success).
nextCursor
: The cursor value to be used for fetching the next page of results.
- The
notes
array contains objects representing individual notes, each with _id
, title
, and content
fields. This structure allows the client to paginate through the notes data efficiently, with the nextCursor
and prevCursor
values helping to navigate between pages.
prevCursor
: The cursor value to be used for fetching the previous page of results.
totalResults
: The total number of notes returned in the current page.
notes
: An array containing the notes fetched based on the provided cursor and limit.
Conclusion
Overall, In this article, we've covered how to paginate with Mongoose in Node.js using both offset-based and cursor-based pagination. Offset-based pagination involves skipping a fixed number of items and limiting the number of items per page.
Cursor-based pagination, on the other hand, uses a cursor to navigate through the dataset, making it more efficient for large datasets. By implementing pagination, we can improve the performance of our Node.js applications when dealing with large amounts of data.
Similar Reads
How to Create Relationships with Mongoose and Node.JS?
Mongoose is a powerful ODM (Object Data Modeling) library for MongoDB and Node.js, allowing developers to define schemas and interact with MongoDB using models. Creating relationships between schemas is important for building complex applications with interrelated data. This article will guide you t
5 min read
How to work Mongojs module in Node.js ?
Mongojs module is a built-in module in node.js that facilitates the usage of MongoDB with Node.js. It offers almost all the functionality that is provided by the official API of MongoDB and is used extensively by developers across the globe. Follow the steps mentioned below to install mongojs module
3 min read
How to Use MongoDB Transactions in Node.js?
Using MongoDB transactions in Node.js involves several steps. Transactions allow multiple operations on the database to be executed in an all-or-nothing manner, ensuring data consistency. we will learn how to use MongoDB transaction in Node.js. PrerequisitesMongoDB (Version 4.0 or higher Recommended
2 min read
How to Perform Text Search in MongoDB using Node.js?
MongoDB is an open-source, cross-platform, No-SQL database that stores data in documents, which contain data in the form of key-value pairs. In this article, we will learn about how to perform text-based searches in MongoDB using node.js. Prerequisites Node.jsMongoDBMongoDB Atlas Connect with Applic
5 min read
How to connect mongodb Server with Node.js ?
mongodb.connect() method is the method of the MongoDB module of the Node.js which is used to connect the database with our Node.js Application. This is an asynchronous method of the MongoDB module. Syntax: mongodb.connect(path,callbackfunction)Parameters: This method accept two parameters as mention
2 min read
How to update record without objectID in mongoose?
Mongoose is an ODM(Object Data Library) for MongoDB in Node JS that helps to write schema, validation and business logic in a simple way without the hassle of native MongoDB boilerplate. PrerequisitesUnderstanding of Mongoose and MongoDBData Modeling and Schema DesignMongoose Query MethodsApproach t
3 min read
How To Set Up Mongoose With Typescript In NextJS ?
Integrating Mongoose with TypeScript in a Next.js application allows you to harness the power of MongoDB while maintaining strong typing and modern JavaScript features. This guide will walk you through the process of setting up Mongoose with TypeScript in a Next.js project, ensuring your application
5 min read
How to Populate Virtuals to a Mongoose Model using Node.js ?
Mongoose virtuals are properties that are not stored in the database but are computed from other document fields. They exist logically within a Mongoose document and allow us to handle derived data, such as full names, formatted dates, or aggregated values, without physically storing them in the dat
5 min read
How to replace one document in MongoDB using Node.js ?
MongoDB, the most popular NoSQL database, we can count the number of documents in MongoDB Collection using the MongoDB collection.countDocuments() function. The mongodb module is used for connecting the MongoDB database as well as used for manipulating the collections and databases in MongoDB. Insta
1 min read
How To Perform a Find Operation With Sorting In MongoDB Using Node.js?
Performing a find operation with sorting in MongoDB using Node.js is a common task for developers working with databases. This guide will walk you through the process step-by-step, including setting up a MongoDB database, connecting to it using Node.js, performing a find operation, and sorting the r
3 min read