npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongo-queryfilter

v0.1.20

Published

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

Downloads

2,336

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