mongo-queryfilter

Safely create a simple mongo query from a querystring collection to allow filtering

npm install mongo-queryfilter
6 downloads in the last day
287 downloads in the last week
1 840 downloads in the last month

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

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

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){
    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}
npm loves you