Generate Mongo queries from querystring parameters
JavaScript
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
test
.gitignore
.travis.yml
HISTORY.md
LICENSE
README.md
index.js
package.json

README.md

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&param2=bar"). Numeric values will be cast as appropriate.

Build Status

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 = require('mongo-queryfilter').filter(querystring, options);

Examples

Simple Equality

var querystring = 'value=bob';
var result = require('mongo-queryfilter').filter(querystring);

Output:

{value: "bob"}

Inequality

var querystring = 'value=__ne_bob';
var result = require('mongo-queryfilter').filter(querystring);

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 = 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']}}

Or

var querystring = 'value=__or_alice||bob';
var result = require('mongo-queryfilter').filter(querystring);

Output:

{value: {$or: ['alice', 'bob']}}

Existence

var querystring = 'value=__exists_true';
var result = require('mongo-queryfilter').filter(querystring);

Output:

{value: {$exists: true}}

ElemMatch

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}]}}

ElemMatch with $in

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}]}}

Define new operators

// 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'}}});

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 = require('mongo-queryfilter').filter(querystring);

Output:

{$and: [{value: {$gt: 1}},{value: {$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 = require('mongo-queryfilter').filter(querystring);

Output:

{$value: {$lte: *Date 2 weeks from now*}}

Boolean Values

var querystring = 'value=__bool_true';
var result = require('mongo-queryfilter').filter(querystring);

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 = 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']}}

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 = require('mongo-queryfilter').filter(querystring);

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 = require('mongo-queryfilter').filter(querystring);

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 = require('mongo-queryfilter').filter(querystring);

Output:

[["dt", -1], ["name", 1]]

Prefixing

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]]