0.2.3 • Published 6 years ago
mongoose-find-and-paginate v0.2.3
mongoose-find-and-paginate
A Mongoose plugin that provides a method, findAndPaginate(), for performing a find and a calculation pagination parameters at the same time.
Install
npm install mongoose-find-and-paginate --saveAPI
findAndPaginate
Parameters
filterObject The filteroptionsObject The Mongoose.Query options. Used for pagination:options.pagenumber The page number (optional).options.perPagenumber The number of documents on page (optional).options.skipnumber The number of documents to skip (optional). Used only if thepageis not set.options.offsetnumber Alias forskip(optional). Used only if thepageis not set.options.limitnumber Maximum number of documents (optional). Used only if thepageis not set.
callbackFunction
Returns Mongoose.Query<QueryResult>
QueryResult
Type: Object
Properties
docsArray<Mongoose.Document> The Array of documentstotalDocsnumber Total number of documents that match a querytotalPagesnumber Total number of pages
Usage
Add plugin
import mongoose from 'mongoose';
import findAndPaginatePlugin from 'mongoose-find-and-paginate';
const schema = new mongoose.Schema({ ... });
schema.plugin(findAndPaginatePlugin);
const Model = mongoose.model('Model', schema);Use skip(offset) & limit
const filter = { ... };
const { docs, totalDocs } = await Model.findAndPaginate(filter, {
skip: 10,
limit: 5,
sort: '_id'
});Use page & perPage
const filter = { ... };
const { docs, totalPages } = await Model.findAndPaginate(filter, {
page: 3,
perPage: 5,
sort: '_id'
});Use with Mongoose.Query methods
const filter = { ... };
const query = Model
.findAndPaginate(filter, { page: 3, perPage: 5 })
.populate(...)
.select(...);
const { docs, totalPages } = await query.exec();