Logical operators $or, $and, $not, $where
You can combine queries using logical operators:
- For
$or
and$and
, the syntax is{ $op: [query1, query2, …] }
. - For
$not
, the syntax is{ $not: query }
- For
$where
, the syntax is{ $where: function () { / object is "this", return a boolean / } }
db.find({ $or: [{ planet: 'Earth' }, { planet: 'Mars' }] }, function (err, docs) {
// docs contains Earth and Mars
});
db.find({ $not: { planet: 'Earth' } }, function (err, docs) {
// docs contains Mars, Jupiter, Omicron Persei 8
});
db.find({ $where: function () { return Object.keys(this) > 6; } }, function (err, docs) {
// docs with more than 6 properties
});
// You can mix normal queries, comparison queries and logical operators
db.find({ $or: [{ planet: 'Earth' }, { planet: 'Mars' }], inhabited: true }, function (err, docs) {
// docs contains Earth
});