Pagination plugin for mongoose.js.
- NodeJS >= 6.0
$ npm install mongoose-simple-pager
// userModel.js
import mongoose from "mongoose";
import paginate from "mongoose-simple-pager";
const userSchema = mongoose.Schema({
firstName: {
type: String,
required: true,
},
lastName: {
type: String,
required: true,
},
userName: {
type: String,
required: true,
unique: true,
},
roles: [
{
type: mongoose.SchemaTypes.ObjectId,
ref: "roles",
},
],
});
userSchema.plugin(paginate);
// controller.js
const User = mongoose.model("userModel");
const options = {
limit: 5,
page: 1,
};
return await User.paginate(options);
{
"data": [
{
"obj1": {...}
},
{
"obj2": {...}
},
{
"obj3": {...}
},
{
"obj4": {...}
},
{
"obj5": {...}
}
],
"pagination": {
"total": 123
}
}
const User = mongoose.model("userModel");
const options = {
limit: 5,
page: 1,
};
const query = {
$or: [
{
userName: {
$regex: "x",
$options: "i",
},
},
],
};
return await User.paginate(options, "roles", query);
- Accept aggregate queries
- Migrate to typescript
- Fork it ( https://github.com/0x3zra/mongoose-simple-pager/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
The MIT License (MIT)