Registry / database / mongoose-middleware

mongoose-middleware

JSON →
library2.0.1jsnpmunverified

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-middleware
INSTALL
IMPORT
SIG · MONGOOSE-MIDDLEWAR
M
mongoose-middleware
databasejavascriptv2.0.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default export (initialize function)
import mongooseMiddleware from 'mongoose-middleware';
const { initialize } = require('mongoose-middleware'); // named import does not work
The package exports a single initialize function as default. CommonJS require('mongoose-middleware') returns an object with an initialize method.
initialize
const mongooseMiddleware = require('mongoose-middleware'); mongooseMiddleware.initialize(mongoose);
require('mongoose-middleware').initialize(mongoose); // this works but uses chaining
initialize() modifies the Mongoose prototype. Must be called once before defining models.
configure options
mongooseMiddleware.initialize({ maxDocs: 1000 }, mongoose);
mongooseMiddleware.initialize(mongoose, { maxDocs: 1000 }); // wrong argument order
First argument is options object, second is mongoose instance. Options can be omitted.

Initializes the plugin, defines a Mongoose model, runs a filtered query with pagination and logs total count and results.

const mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/test'); const Schema = mongoose.Schema; const Kitten = mongoose.model('Kitten', new Schema({ name: String, color: String })); require('mongoose-middleware').initialize(mongoose); const options = { filters: { mandatory: { exact: { color: 'black' } } }, start: 0, count: 10 }; Kitten.find() .filter(options) .page(options) .then(results => { console.log('Total:', results.total); console.log('Kittens:', results.results); }) .catch(err => console.error(err));
Debug
Known issues
gotchainitialize() must be called before defining any models or registering plugins, otherwise the query prototype modifications won't apply.
fix
Call initialize(mongoose) immediately after requiring mongoose, before any mongoose.model() calls.
affects: >=1.0.0
gotchaFilters use special keys like 'mandatory', 'optional', 'contains', 'exact'. The structure is non-obvious and differs from Mongoose's native query API.
fix
Refer to the README for the exact filter object shape. Example: { filters: { mandatory: { exact: { field: 'value' } } } }
affects: >=1.0.0
gotchaThe .field(), .keyword(), .filter(), .order() methods must be called in sequence before .page(). They rely on internal state set by previous calls.
fix
Always chain .field().keyword().filter().order().page() in that order, or call .page() alone with options containing all filter/sort/pagination info.
affects: >=1.0.0
gotchaThe .page() method returns a promise only if no callback is provided. If a callback is given, it uses the old Node.js callback pattern.
fix
Omit the callback argument to get a promise: .page(options).then(...) instead of .page(options, callback).
affects: >=2.0.0
gotchaThe package modifies Mongoose's query prototype directly. This can conflict with other plugins or future Mongoose versions.
fix
Use with caution. Consider alternative approaches like mongoose-query-paginate or manual query building.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: mongoose.Query.prototype.filter is not a function
initialize() was not called or was called after model creation.
fix
Ensure require('mongoose-middleware').initialize(mongoose); is executed before any mongoose.model() call.
Cannot read property 'page' of undefined
.page() called without previous chained methods or options argument missing.
fix
Chain .page(options) after .find() (or other query builder). Options must contain at least { start: 0, count: 10 }.
MongooseError: Query was already executed
Calling .page() after the query has been executed (e.g., after .exec() or .then()).
fix
Do not mix .exec() or explicit then/catch with .page(). Use .page() as the final call in the chain.
Upgrade
Version history
2.0.1latest on npm
Audit
Dependencies
mongooserequiredpeer dependency – the plugin wraps Mongoose query methods
Agent activity
5 hits · last 30 days
node
4
Resources
mongoose-middleware — npm install mongoose-middleware · libregistry