mongo-queryfilter
Generate an object suitable for use as a a mongo query from a querystring or request object.
The filter function takes either a raw object, a node http request object or a string containing the "query" portion of the request (e.g. "param=foo¶m2=bar"). Numeric values will be cast as appropriate.
Options
var querystring = '_p_value=bob';var options = "prefix": "_p_" // Optional prefix for the key names. If present only keys starting with the prefix will be passed through;var result = ;
Examples
Simple Equality
var querystring = 'value=bob';var result = ;
Output:
value: "bob"
Inequality
var querystring = 'value=__ne_bob';var result = ;
Output:
value: $ne: "bob"
Greater Than
var querystring = 'value=__gt_5;var result = require('mongo-queryfilter').filter(querystring);
Output:
value: $gt: 5
The same applies to "_lt" "_gte" "_lte" (<, >= AND <=)
In
var querystring = 'value=__in_alice||bob';var result = ;
Output:
value: $in: 'alice' 'bob' ### All```javascriptvar querystring = 'value=__all_alice||bob';var result = require('mongo-queryfilter').filter(querystring);
Output:
value: $all: 'alice' 'bob'
Or
var querystring = 'value=__or_alice||bob';var result = ;
Output:
value: $or: 'alice' 'bob'
Existence
var querystring = 'value=__exists_true';var result = ;
Output:
value: $exists: true
ElemMatch
var querystring = 'array="__elemMatch_alice__eq_a,bob__gt_1';var result = ;
Output:
array: $elemMatch: alice: 'a' bob: $gt: 1
ElemMatch with $in
var querystring = 'array="__elemMatch_id__in_a||b,bob__gt_1';var result = ;
Output:
array: $elemMatch: id: $in: 'a' 'b' bob: $gt: 1
Range
Filters for values between two numbers.
var querystring = 'value="__range_10_20';var result = ;
Output:
value: $gte: 10 $lt: 20
Not in Range
Filters for values not between two numbers.
var querystring = 'value="__nrange_10_20';var result = ;
Output:
value: $not: $gte: 10 $lt: 20
Define new operators
// define a function that returns an operator $thing, that doubles the valuevar { return value * 2;}; var result = ;
Output:
"extra.color": "red" price: $thing: 4} // Note that $thing isn't valid in mongo, just an example - this will allow you to inject more complicated operators
Multiple Conditions
You can apply multiple conditions to the same key to create ranges:
var querystring = 'value=__gt_1&value=__lt_10';var result = ;
Output:
$and: value: $gt: 1value: $lt: 10
Date Filtering
You can filter based on both relative and absolute dates. There are the following date comparisons: dtgt,dtlt,dtgte,dtlte, dteq. You can pass an absolute value in ISO8601 format (e.g. 2014-01-01) or as a timestamp (1388534400000)
Alternatively, you can pass a relative value e.g. "now" "5minutes" "1hour" "-2days" "4weeks", "1year"
var querystring = 'value=__dtlte_2weeks';var result = ;
Output:
$value: $lte: *Date 2 weeks from now*
Boolean Values
var querystring = 'value=__bool_true';var result = ;
Output:
value: true
Casting to a string
Sometimes, it is desirable to not cast to a number (e.g. 0 prefixed numeric strings)
var querystring = 'value=__streq_007';var result = ;
Output:
value: "007"
The same can be done for $in and $nin:
var querystring = 'value=__strin_007||008';var result = ;
Output:
value: $in: '007' '008'
Sorting
The module can also handle the generation of the sorting parameter for you. Values of the "sort" parameter will be used from the querystring. A prefix can be used to avoid clashes.
var querystring = 'sort=dt__-1';var result = ;
Output:
dt -1
Multiple Sorts
Duplicate the sort parameter, in the order you wish the sort to occur in:
var querystring = 'sort=dt__-1&sort=name__1';var result = ;
Output:
"dt" -1 "name" 1
Asc / Dec
"asc" and "desc" are also supported as values
var querystring = 'sort=dt__desc&sort=name__asc';var result = ;
Output:
"dt" -1 "name" 1
Prefixing
A prefix can be specified
var querystring = '__sort=dt__-1&__sort=name__1&sort=ignored__1';var result = ;
Output:
"dt" -1 "name" 1