Accept MongoDB query parameters through URI queries safe and easy. This is useful when building an API and accepting various user specificed queries.
- Aliased query parameters
- Blacklisted query parameters
- Whitelisted query parameters
- Basic operators
$ne$gt$lt$regex$exists
- Geospatial operators
$geoWithin$near
- Custom query functions
npm install mongo-querystring --save
var MongoQS = require('mongo-querystring');Arrayops - list of supported operatorsobjectalias - query param aliasesobjectblacklist - blacklisted query paramsobjectwhitelist - whitelisted query paramsobjectcustom - custom query params
bbox- bounding box querynear- proximity queryafter- modified since query
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';
}
}
});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
});
