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 --save
API
findAndPaginate
Parameters
filter
Object The filteroptions
Object The Mongoose.Query options. Used for pagination:options.page
number The page number (optional).options.perPage
number The number of documents on page (optional).options.skip
number The number of documents to skip (optional). Used only if thepage
is not set.options.offset
number Alias forskip
(optional). Used only if thepage
is not set.options.limit
number Maximum number of documents (optional). Used only if thepage
is not set.
callback
Function
Returns Mongoose.Query<QueryResult>
QueryResult
Type: Object
Properties
docs
Array<Mongoose.Document> The Array of documentstotalDocs
number Total number of documents that match a querytotalPages
number Total number of pages
Usage
Add plugin
;; const schema = ... ;schema;const Model = mongoose;
Use skip(offset) & limit
const filter = ... ;const docs totalDocs = await Model;
Use page & perPage
const filter = ... ;const docs totalPages = await Model;
Use with Mongoose.Query methods
const filter = ... ;const query = Model ; const docs totalPages = await query;