This is a simple package that paginates your mongodb collection.It uses the mongoose ORM behind the scene.
npm install express-mongo-paginator
const paginator = require("express-mongo-paginator")
This creates a new instance of express-mongo-paginator
collection
:This is the collection you want to paginate;currentpage
: Current page;perpage
: Number of items in a collection to be displayed perpage;filterConditions
: Add querys to your collection.If there aren't any filter condition pass and empty array[]
.IdToBePopulated
: Document To be populated.
const express = require('express');
const paginator = require("express-mongo-paginator");
const mongoose = require("mongoose")
const PORT = process.env.PORT || 5000;
const app = express();
app.get("/get-users", async (req, res) => {
try {
let data = await paginator.paginator(User, page, 2,[{
name: req.query.name
}],"SchoolId,CategoryId");
res.status(200).json({data})
} catch (err) {
res.status(500).json(err)
}
})
app.listen(PORT. () => {
logger.info(`App is running on ${port}`)
})