Mongoose Middleware is a plugin for Mongoose (Mongoose ODM) that adds chainable query methods for filtering, sorting, pagination, and field projection. Version 2.0.1 is the latest stable release. It provides a simple, promise-supporting API for common query operations, reducing boilerplate code when building REST APIs. The package exposes methods like .field(), .keyword(), .filter(), .order(), and .page() on Mongoose Query objects. It supports mandatory/optional filters, keyword search, sorting, and pagination with total count. The plugin must be initialized with a Mongoose instance and optionally configured with a maxDocs limit.
npm install mongoose-middlewareNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Initializes the plugin, defines a Mongoose model, runs a filtered query with pagination and logs total count and results.
Call initialize(mongoose) immediately after requiring mongoose, before any mongoose.model() calls.
Refer to the README for the exact filter object shape. Example: { filters: { mandatory: { exact: { field: 'value' } } } }Always chain .field().keyword().filter().order().page() in that order, or call .page() alone with options containing all filter/sort/pagination info.
Omit the callback argument to get a promise: .page(options).then(...) instead of .page(options, callback).
Use with caution. Consider alternative approaches like mongoose-query-paginate or manual query building.
Ensure require('mongoose-middleware').initialize(mongoose); is executed before any mongoose.model() call.Chain .page(options) after .find() (or other query builder). Options must contain at least { start: 0, count: 10 }.Do not mix .exec() or explicit then/catch with .page(). Use .page() as the final call in the chain.