MongoDB — Logical Operators
Combine multiple search conditions using $and, $or, $not, and $nor logical query operators.
1Logical Operators Summary
$and: Joins query clauses with a logical AND (all conditions must be true).$or: Joins query clauses with a logical OR (at least one condition must be true).$not: Inverts the effect of a query expression.$nor: Joins query clauses with a logical NOR (all conditions must fail).
2Practical Examples
$or Query Example
// Find users who are admins OR have score greater than 90
db.users.find({
$or: [
{ role: "admin" },
{ score: { $gt: 90 } }
]
});
$and Query Example
// Explicit $and (required when applying multiple conditions to same field)
db.products.find({
$and: [
{ price: { $gt: 10 } },
{ price: { $lt: 50 } }
]
});