Skip to content
This repository was archived by the owner on May 1, 2025. It is now read-only.

Query builder for accepting URL query parameters into your MongoDB queries. Safe and feature rich. Supports most of MongoDB's query operators such as $eq, $gt, $lt, $ne, $in, $nin, $exists, $regex, geospatial queries such as bbox and near, as well as your own custom query business logic!

License

Notifications You must be signed in to change notification settings

Turistforeningen/node-mongo-querystring

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MongoDB QueryString

Build status NPM downloads NPM version Node version Dependency status

Accept MongoDB query parameters through URI queries safe and easy. This is useful when building an API and accepting various user specificed queries.

Features

  • Aliased query parameters
  • Blacklisted query parameters
  • Whitelisted query parameters
  • Basic operators
    • $eq
    • $gt
    • $lt
    • $ne
    • $in
    • $nin
    • $exists
    • $regex
  • Parse string integers and floats to numbers
  • Parse string boolean to ture/false booleans
operation query string query object
equal ?foo=bar { foo: "bar" }
unequal ?foo=!bar { foo: { $ne: "bar" }}
exists ?foo= { foo: { $exists: true }}
not exists ?foo=! { foo: { $exists: false }}
greater than ?foo=>10 { foo: { $gt: 10 }}
less than ?foo=<10 { foo: { $lt: 10 }}
starts with ?foo=^bar { foo: { $regex: "^bar", $options: "i" }}
ends with ?foo=$bar { foo: { $regex: "bar$", $options: "i" }}
contains ?foo=~bar { foo: { $regex: "bar", $options: "i" }}
in array ?foo[]=bar&foo[]=baz { foo: { $in: ['bar', 'baz'] }}
not in array ?foo[]=!bar&foo[]=!baz { foo: { $nin: ['bar', 'baz'] }}
  • Geospatial operators
    • $geoWithin (polygon)
    • $near (point)
operation query string query object
bbox ?bbox=0,1,2,3 { geojson: { $geoWithin: { $geometry: { … } } } }
near ?near=0,1 { geojson: { $near: { $geometry: { … } } } }
  • Custom query functions
    • after (date)
operation query string query object
after ?after=2014-01-01 { endret: { $gte: "2014-01-01T00:00:00.000Z" } }
after ?after=1388534400 { endret: { $gte: "2014-01-01T00:00:00.000Z" } }

Install

npm install mongo-querystring --save 

API

var MongoQS = require('mongo-querystring');

new MongoQS(object options)

  • Array ops - list of supported operators
  • object alias - query param aliases
  • object blacklist - blacklisted query params
  • object whitelist - whitelisted query params
  • object custom - custom query params
  • object string - string parsing
    • boolean toBoolean - parse "true", "false" string to booleans
    • boolean toNumber - parse string integer and float values to numbers

Bult in custom queries

  • bbox - bounding box geostatial query
  • near - proximity geostatial query
  • after - modified since query
var qs = new MongoQS({ custom: { bbox: 'geojson', // your geometry field near: 'geojson', // your geometry field after: 'updated_on' // your last modified field } });

Define custom queries

Custom queries are on the folling form; you define the URL query parameter name that your users will be using and a function which takes the result query object and the value for query parameter.

var qs = new MongoQS({ custom: { urlQueryParamName: function(query, input) { // do some processing of input value // add your queries to the query object query['someField'] = input; query['someOtherFiled'] = 'some value'; } } });

qs.parse(object params)

Params is an object with URI query params and their values. Ex. req.params if you are working with ExpressJS.

var query = qs.parse(req.params); mongo.collection('mycol').find(query, field).toArray(function(err, documents) { // matching documents });

About

Query builder for accepting URL query parameters into your MongoDB queries. Safe and feature rich. Supports most of MongoDB's query operators such as $eq, $gt, $lt, $ne, $in, $nin, $exists, $regex, geospatial queries such as bbox and near, as well as your own custom query business logic!

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 6