Mongo query helper for common, repetitive queries. Easier to write and saves some typing.
$ npm install mongo-query-helper
const mh = require('mongo-query-helper');
//mongoose
User.find({accessLevel: mh.ne(2)}, function(err, users){
if(!err){
console.log(users)
}
});
Match.find({completed: mh.exists(), score: mh.gtLt(32, 100)}, function(err, matches){
if(!err){
console.log(matches)
}
});
Less than
mh.lt(2)
// { '$lt': 2}
Less than equal to
mh.lte(2)
// { '$lte': 2}
Greater than
mh.gt(2)
// { '$gt': 2}
Greater than equal to
mh.gte(2)
// { '$gte': 2}
Greater than and less than
mh.gtLt(2, 4)
// { '$gt': 2, '$lt': 4}
Greater than equal to and less than
mh.gteLt(2, 4)
// { '$gte': 2, '$lt': 4}
Greater than and less than equal to
mh.gtLte(2, 4)
// { '$gt': 2, '$lte': 4}
Greater than equal to and less than equal to
mh.gteLte(2, 4)
// { '$gte': 2, '$lte': 4}
mh.exists()
// { '$exists': true}
mh.notExists()
// { '$exists': false}
Equal to
mh.eq(7)
// { '$eq': 7}
Not equal to
mh.ne(7)
// { '$ne': 7}
mh.in([1, 2, 3])
// OR
mh.in(1, 2, 3)
// { '$in': [1, 2, 3]}
Not in
mh.nin([1, 2, 3])
// OR
mh.nin(1, 2, 3)
// { '$nin': [1, 2, 3]}
mh.mod(4, 0)
// { '$mod': [4, 0]}
Merges all the object parameters into one. Shorter name mrg
mh.mrg(mh.exists(), mh.ne(2), mh.gteLte(1, 10))
// { '$exists': true, '$ne': 2, '$gte': 1, '$lte': 10}
- mongo-date-query helper for common mongo date queries
MIT © Talha Awan