Mongoose Paginate for JavaScript (Node.js): Complete Guide with Examples
\n\nWelcome to PythonLib! Today we will cover Mongoose Paginate — one of the most popular libraries for implementing pagination in Node.js applications using Mongoose ODM (Object Data Modeling) for MongoDB.
\n\n1. What It Is and Why You Need It
\n\nMongoose Paginate is a lightweight plugin for Mongoose that adds the paginate() method to your schemas. It allows you to easily split large datasets into pages, avoiding loading all documents at once.
Why is this needed? In real applications (blogs, online stores, APIs), databases contain thousands, sometimes millions of records. Loading them all at once means:
\n- \n
- Huge server load (memory, CPU). \n
- Slow API performance (user waits minutes for a response). \n
- Poor UX (infinite scroll without pagination or "Next" buttons). \n
Mongoose Paginate solves these problems by providing a ready-made, tested, and flexible pagination implementation. It automatically counts the total number of documents, returns the requested page, and provides meta-information (current page, total pages, whether there is a next/previous page).
\n\n2. Installation
\n\nInstallation is done via npm (Node Package Manager). Make sure you already have Node.js and npm installed.
\n\nnpm install mongoose-paginate-v2\n\nImportant: There are two versions of the library:
\n- \n
mongoose-paginate— old, less maintained version. \n mongoose-paginate-v2— current, recommended version with more features and fixes. \n
In this article, we will use mongoose-paginate-v2.
\n\n3. Quick Start — Minimal Working Example
\n\nLet's create a simple "User" model and apply pagination to it.
\n\n// 1. Import dependencies\nconst mongoose = require('mongoose');\nconst mongoosePaginate = require('mongoose-paginate-v2');\n\n// 2. Define the schema\nconst userSchema = new mongoose.Schema({\n name: String,\n email: String,\n age: Number,\n createdAt: { type: Date, default: Date.now }\n});\n\n// 3. Add the pagination plugin to the schema\nuserSchema.plugin(mongoosePaginate);\n\n// 4. Create the model\nconst User = mongoose.model('User', userSchema);\n\n// 5. Connect to MongoDB (example with local DB)\nasync function main() {\n await mongoose.connect('mongodb://localhost:27017/testdb');\n\n // 6. Insert test data (if none exists)\n const count = await User.countDocuments();\n if (count === 0) {\n const users = [];\n for (let i = 1; i <= 50; i++) {\n users.push({ name: `User ${i}`, email: `user${i}@example.com`, age: 20 + (i % 30) });\n }\n await User.insertMany(users);\n console.log('Inserted 50 users');\n }\n\n // 7. Use pagination\n const options = {\n page: 1, // Page number (default 1)\n limit: 10, // Number of items per page (default 10)\n };\n\n const result = await User.paginate({}, options);\n\n console.log('Pagination result:');\n console.log('Documents:', result.docs);\n console.log('Total documents:', result.totalDocs);\n console.log('Total pages:', result.totalPages);\n console.log('Current page:', result.page);\n console.log('Has next page:', result.hasNextPage);\n console.log('Has previous page:', result.hasPrevPage);\n\n await mongoose.disconnect();\n}\n\nmain().catch(console.error);\n\n\nThis code will create 50 users, then request the first page with 10 items.
Also in library
CLI frameworks for Java: overview and comparison of the best tools
Working with ZIP Archives in Go (Golang): Creating, Reading, and Extracting Files
Working with Markdown in Rust: Libraries and Tools
Working with Images in Rust: The image Library and Beyond
Object Serialization in Swift (iOS/macOS): Codable, JSON and PropertyList