Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

mongo-queryfilter

dhendo2.1kMIT0.1.20

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

readme

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

Range

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

Not in Range

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 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', "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

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

changelog

History

2020-08-04 v0.1.20

Allow the result to be returned in the $and form even if there is only one condition by passing {forceand: true} in the options @dhendo

2020-08-04 v0.1.19

Dependabot upgrade of qs @dhendo

2020-08-04 v0.1.18

Allow operators to request to be returned without a namespace. useful for e.g. $text @dhendo

2019-08-08 v0.1.16

Add "range" and "nrange" operators @jake314159

2016-11-18 v0.1.15

Allow extended operators to specify their own clean function @jake314159

2016-02-11 v0.1.14

Apply rename when using multiple filters @jake314159

2015-10-26 v0.1.13

Bumped qsversion @dhendo

2015-09-25 v0.1.12

Disallow nulls for certain operators @jake314159

2015-08-12 v0.1.11

Pass fieldname to operator function @jake314159

2015-06-16 v0.1.10

Add support for $all

2015-02-19 v0.1.9

Bugfixes to allow raw objects to pass in arrays / numbers correctly

2015-02-18 v0.1.7

Allow a raw object to be passed in. Flatten deep object properties.

2014-11-12 v0.1.5

Allow custom operators to inline a $not to negate the section of the query

2014-10-20 v0.1.4

Allow custom operators to override the key of the field that is being operated on.

2014-09-05 v0.1.3

Added strin and strnin operators to avoid casting to a number (e.g. "007" does not get casted to 7) for in queries

2014-04-21 v0.1.2

Added streq operator to avoid casting to a number (e.g. "007" does not get casted to 7)

2014-04-24 v0.1.1

Switched order separator from _ to __

2014-04-24 v0.1.0

Added sort function. Added testing on Travis.

2014-04-16 v0.0.9

Add in coercion to booleans with _bool.

2014-03-19 v0.0.8

Fix relative date filtering - was returning a timestamp rather than a date object

2014-02-19 v0.0.7

Add relative and absolute date filtering Add support for $exists operator

2014-02-19 v0.0.6

Add nin operator (MikeTheTechie)

2014-01-07 v0.0.5

Pass the operator through to custom filters

2014-01-07 v0.0.4

Added the ability to generate an elemMatch Added the ability to inject operators. This allows for more complicated expansions / aliases / calculations.

2013-10-24 v0.0.3

Added fix for dotted notation.

2013-10-23 v0.0.2

Allow multiple conditions to be applied to the same key.

2013-10-22 v0.0.1

Initial version