Dark Mode

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

query-easy/mongo

Repository files navigation

@query-easy/mongo

A query builder for MongoDB, built to provide a rich and dynamic query building interface.

Contents

  • Examples
  • Working with mongodb and mongoose
  • Full API matrix

Examples

Getting Started

{ cb.in('fav_restaurants', ['chillis', 'benihana']) }) .all('hobbies', ['parkour', 'rap']) .regex('prison_name', new RegExp('prisonmike', 'i')) .offset(5) .raw('age', '>=', 96) .exists('department', true) .raw('sort', ['name', 'age']) .toQuery(); /* query = { filter: { character_name: 'Michael Scott', $or: [ {fav_restaurants: {'$in': ['chillis', 'benihana']}}, ] hobbies: {'$all': ['parkour', 'rap']}, prison_name: {'$regex': /prisonmike/i}, age: {'$gte': 96}, department: {'$exists': true} }, options: { skip: 5, sort: {name: 1, age: 1} } } */">const qe = require('@query-easy/mongo');
const query = qe('office_collection')
.eq('character_name', 'Michael Scott')
.or((cb) => {
cb.in('fav_restaurants', ['chillis', 'benihana'])
})
.all('hobbies', ['parkour', 'rap'])
.regex('prison_name', new RegExp('prisonmike', 'i'))
.offset(5)
.raw('age', '>=', 96)
.exists('department', true)
.raw('sort', ['name', 'age'])
.toQuery();

/*
query = {
filter: {
character_name: 'Michael Scott',
$or: [
{fav_restaurants: {'$in': ['chillis', 'benihana']}},
]
hobbies: {'$all': ['parkour', 'rap']},
prison_name: {'$regex': /prisonmike/i},
age: {'$gte': 96},
department: {'$exists': true}
},
options: {
skip: 5, sort: {name: 1, age: 1}
}
}
*/

More examples here

Working with mongodb and mongoose

{ cb.in('fav_restaurants', ['chillis', 'benihana']) }) .all('hobbies', ['parkour', 'rap']) .regex('prison_name', new RegExp('prisonmike', 'i')) .offset(5) .raw('age', '>=', 96) .exists('department', true) .raw('sort', ['name', 'age']) .toQuery(); // MongoDB Ex. let collection = mongodb().collection('office_collection'); collection.find( query.filter, query.options ).toArray((err, docs) => { // do some stuff... }) // Mongoose Ex. let filter = query.filter; let options = query.options; // Following line is added because mongoose find() does not want projection in options let projection = query.options.projection; delete options.projection; MongooseModel.find(filter, projection, options, (err, docs) => { // do some stuff... })">const qe = require('@query-easy/mongo');
const query = qe('office_collection')
.eq('character_name', 'Michael Scott')
.or((cb) => {
cb.in('fav_restaurants', ['chillis', 'benihana'])
})
.all('hobbies', ['parkour', 'rap'])
.regex('prison_name', new RegExp('prisonmike', 'i'))
.offset(5)
.raw('age', '>=', 96)
.exists('department', true)
.raw('sort', ['name', 'age'])
.toQuery();

// MongoDB Ex.
let collection = mongodb().collection('office_collection');
collection.find(
query.filter,
query.options
).toArray((err, docs) => {
// do some stuff...
})

// Mongoose Ex.
let filter = query.filter;
let options = query.options;

// Following line is added because mongoose find() does not want projection in options
let projection = query.options.projection;
delete options.projection;

MongooseModel.find(filter, projection, options, (err, docs) => {
// do some stuff...
})

Full API matrix

Refer API.md

About

A query builder for MongoDB, built to provide a rich and dynamic query building interface.

Topics

Resources

Readme

License

MIT license

Stars

Watchers

Forks

Packages

Contributors