mongodb-querystring

1.0.3 • Public • Published

MongoDB query through URL

Pass MongoDB query through URL query string.

Contributors

Installation

With npm do:

$ npm install mongodb-querystring

Tests

Use mocha to run the tests.

$ npm install mocha
$ mocha test

API Documentation

stringify(input : Object, [separator : String]) : String

Converts query to a URL query string.

    var mqs = require('mongodb-querystring');
    var qry = {
     "$query" : {
       "age" : {
         "$gte" : new Date(1976, 11, 14)
       },
       "$or": [
         { "cuisine": "Italian" },
         { "address.zipcode": "10075" }
       ]
     },
     "$limit" : 20,
     "$orderby" : {
       "age" : -1,
       "posts": 1
     }
   };
   console.log(mqs.stringify(qry);
 

Output:

'$query[age][$gte]=219366000000^D&$query[$or][0][cuisine]=Italian&$query[$or][1][address.zipcode]=10075&$limit=20^N&$orderby[age]=-1^N&$orderby[posts]=1^N'

parse(input : String) : Object

parse(input : Object) : Object

Parse input to convert to an query.

    var mqs = require('mongodb-querystring');
    var qry = '$query[type][$in][0]=food&$query[type][$in][1]=snacks';
    console.dir(mqs.parse(qry));
 

Output:

{ '$query': { type: { '$in': [ 'food', 'snacks' ] } } }

create(input : String) : Object

create(input : Object) : Object

Parse input to convert to an Object query.

    var mqs = require('mongodb-querystring');
    var qry = mqs.create(req.query);
    console.log(qry.$query());
    console.log(qry.$limit(10));
 

Also

Package Sidebar

Install

npm i mongodb-querystring

Weekly Downloads

0

Version

1.0.3

License

MIT

Last publish

Collaborators

  • touv