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.
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 = require('mongo-queryfilter').filter(querystring, options);
var querystring = 'value=bob';
var result = require('mongo-queryfilter').filter(querystring);
Output:
{value: "bob"}
var querystring = 'value=__ne_bob';
var result = require('mongo-queryfilter').filter(querystring);
Output:
{value: {$ne: "bob"}}
var querystring = 'value=__gt_5;
var result = require('mongo-queryfilter').filter(querystring);
Output:
{value: {$gt: 5}}
The same applies to "_lt" "_gte" "_lte" (<, >= AND <=)
var querystring = 'value=__in_alice||bob';
var result = require('mongo-queryfilter').filter(querystring);
Output:
{value: {$in: ['alice', 'bob']}}
### All
```javascript
var querystring = 'value=__all_alice||bob';
var result = require('mongo-queryfilter').filter(querystring);
Output:
{value: {$all: ['alice', 'bob']}}
var querystring = 'value=__or_alice||bob';
var result = require('mongo-queryfilter').filter(querystring);
Output:
{value: {$or: ['alice', 'bob']}}
var querystring = 'value=__exists_true';
var result = require('mongo-queryfilter').filter(querystring);
Output:
{value: {$exists: true}}
var querystring = 'array="__elemMatch_alice__eq_a,bob__gt_1';
var result = require('mongo-queryfilter').filter(querystring);
Output:
{array: {$elemMatch: {alice: 'a', bob: {$gt: 1}]}}
var querystring = 'array="__elemMatch_id__in_a||b,bob__gt_1';
var result = require('mongo-queryfilter').filter(querystring);
Output:
{array: {$elemMatch: {id: {$in: ['a', 'b']}, bob: {$gt: 1}]}}
Filters for values between two numbers.
var querystring = 'value="__range_10_20';
var result = require('mongo-queryfilter').filter(querystring);
Output:
{value: {$gte: 10, $lt: 20}}
Filters for values not between two numbers.
var querystring = 'value="__nrange_10_20';
var result = require('mongo-queryfilter').filter(querystring);
Output:
{value: {$not: {$gte: 10, $lt: 20}}}
// define a function that returns an operator $thing, that doubles the value
var thingfn = function(value, helpers, operatorName, fieldName){
return value * 2;
};
var result = require('mongo-queryfilter').filter('extra.color=red&price=__thing_2', {operators: {'thing': {'fn': thingfn, 'ns': '$thing', "rawvalue": false}}});
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
You can apply multiple conditions to the same key to create ranges:
var querystring = 'value=__gt_1&value=__lt_10';
var result = require('mongo-queryfilter').filter(querystring);
Output:
{$and: [{value: {$gt: 1}},{value: {$lt: 10}}]}
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 = require('mongo-queryfilter').filter(querystring);
Output:
{$value: {$lte: *Date 2 weeks from now*}}
var querystring = 'value=__bool_true';
var result = require('mongo-queryfilter').filter(querystring);
Output:
{value: true}
Sometimes, it is desirable to not cast to a number (e.g. 0 prefixed numeric strings)
var querystring = 'value=__streq_007';
var result = require('mongo-queryfilter').filter(querystring);
Output:
{value: "007"}
The same can be done for $in and $nin:
var querystring = 'value=__strin_007||008';
var result = require('mongo-queryfilter').filter(querystring);
Output:
{value: {$in: ['007', '008']}}
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 = require('mongo-queryfilter').filter(querystring);
Output:
[[dt, -1]]
Duplicate the sort parameter, in the order you wish the sort to occur in:
var querystring = 'sort=dt__-1&sort=name__1';
var result = require('mongo-queryfilter').filter(querystring);
Output:
[["dt", -1], ["name", 1]]
"asc" and "desc" are also supported as values
var querystring = 'sort=dt__desc&sort=name__asc';
var result = require('mongo-queryfilter').filter(querystring);
Output:
[["dt", -1], ["name", 1]]
A prefix can be specified
var querystring = '__sort=dt__-1&__sort=name__1&sort=ignored__1';
var result = require('mongo-queryfilter').filter(querystring, {"prefix": "__"});
Output:
[["dt", -1], ["name", 1]]