MongoDB logical operators: $and, $or, $not
$and, $or, $not, $nor β and when you don't need them. Β· 8 practice questions Β· Cheat sheet β
MongoDB ANDs conditions together by default: { borough: "Manhattan", price: 4 } already means both. You only need $and when the same field appears twice or when you are combining $or blocks. $or matches if any clause does, $nor if none do, and $not negates a single operator expression on one field rather than a whole query.
A typical $and $or query example nests them: { $and: [{ $or: [...] }, { $or: [...] }] }. The common mistakes are wrapping everything in $and when implicit AND would do, and using $not with a plain value, which MongoDB rejects because $not expects an operator document such as { $gt: 3 }. For excluding a plain value, use $ne.
MongoDB $and and $or practice questions
- Find restaurants in
Manhattanwhosepriceis 4, writing the condition with an explicit$and. easy restaurants - Find restaurants in either
BronxorStaten Islandusing$or. easy restaurants - Find restaurants that are in neither
ManhattannorBrooklyn, using$nor. medium restaurants - Find restaurants whose
priceis NOT greater than 2, using$not. medium restaurants - Find restaurants that are open (
isOpentrue) AND are either Indian or Italian. medium restaurants - Find restaurants that are NOT American and NOT Pizza, OR whose
namestarts with the letter B. hard restaurants - Find movies rated
Rthat belong to either the Action or Sci-Fi genre. hard movies - Find restaurants that are closed (
isOpenfalse) or have fewer than 20 seats. hard restaurants
Each question is answered in a live mongo-shell editor on this page; the result is checked against a reference solution.
$and, $or, $not and $nor cheat sheet
Implicit AND is free
// these two are identical
db.restaurants.find({ borough: "Manhattan", price: 4 })
db.restaurants.find({ $and: [ { borough: "Manhattan" }, { price: 4 } ] })
You only need $and when the same field appears twice with operators that
would collide as duplicate keys.
The four
{ $or: [ { a: 1 }, { b: 2 } ] } // at least one true
{ $and: [ { a: 1 }, { b: 2 } ] } // all true
{ $nor: [ { a: 1 }, { b: 2 } ] } // none true
{ price: { $not: { $gt: 2 } } } // wraps an operator, not a value
$not is different
$not is a field-level operator β it goes inside a field, wrapping
another operator expression. It cannot take a plain value:
{ price: { $not: 2 } } // β error
{ price: { $not: { $eq: 2 } } } // β
Gotchas
$norand$notmatch documents where the field is absent.- Mixing: put
$orinside$andwhen you need "X and (Y or Z)".
Common questions
When do I actually need $and in MongoDB?
{ $and: [{ price: { $gt: 1 } }, { price: { $lt: 4 } }] } (one object with both bounds is nicer), or when combining several $or groups. Otherwise listing fields side by side is already an AND.Why can't I use $not with a plain value?
$not negates an operator expression, so it needs something like { seats: { $not: { $gt: 50 } } }. For "not equal to this value" use $ne. Note that $not also matches documents where the field is missing, exactly like $ne does.What's the difference between $nor and $not?
$nor takes an array of whole conditions and matches documents that satisfy none of them: { $nor: [{ borough: "Manhattan" }, { borough: "Brooklyn" }] } excludes both boroughs. $not applies to a single field's operator expression.Continue:β Comparison OperatorsElement & Type β
MongoDB topics:Finding & ProjectionComparison OperatorsElement & TypePattern MatchingSort, Limit & SkipArray QueriesEmbedded DocumentsAggregation BasicsGrouping & Accumulators$unwind & Array PipelinesJoins & Advanced StagesFull cheat sheet