Helper for parsing mongoose mongodb query operators.
What is this?
get-query-modifier is a module designed to help with mongoose query parsing.
Let's say you have a
GET /api/comments endpoint, in your API like the
Suddenly, you decided to add pagination to this route. You could easily
implement calls with mongoose's
Query.prototype.skip, as follows:
Or something like that.
You'd be, however, recreating your own query syntax.
You can get a lot more power by using mongodb's default query syntax,
extending it where mongoose/mongodb expose a slightly different API, such
get-query-modifier solves this solution by extracting the operators
$select (plus any operator you want to
support by setting
options.allow), and translating them into a set of method
calls to the query object (calls to
Query.prototype.select etc.). This allows maximum flexibility for using both
req.query object for further querying in the mongoose API and supporting a
known standard set of operators, which come out-of-the-box in mongodb.
It does it by taking the query object as its first argument and returning a
modifier function, which handles calling each of these deferred
calls once your
Query instance is available.
Manipulates an object representation of a querystring, extracting its
mongoose query operators and returning a function to attach them to a
Note this function isn't pure and does delete the operators from the
parameter after reading them.
- Object query The querystring representation to manipulate
- Object [options] An options object
- Object [options.defaults] An object with defaults for the valid operators
- Object [options.ignore] An object with the properties corresponding to the ignored operators set to true
- Boolean [options.deleteIgnored=false] Whether to delete the ignored
operators from the
- Array. [options.allow] An array of operators to parse in
addition to the plugin's default
- Function queryModifier The
mongoose.Querymodifier function, which attaches the operators to the search
var app = ; // some express appvar mongoose = ;var User = mongoose; // some mongoose modelapp;
See the tests for more elaborate examples.
A connect middleware for parsing and using mongodb query operators with mongoose given an object representation of a query.
- Object [options] An options object which will simply be passed onto
- Function mdw The middleware function
var app = ; // some express appapp;
This code is licensed under the MIT License. See LICENSE for more information.