πŸƒDBQuestpractice MongoDB queries
0 / 110

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

  1. Find restaurants in Manhattan whose price is 4, writing the condition with an explicit $and. easy restaurants
  2. Find restaurants in either Bronx or Staten Island using $or. easy restaurants
  3. Find restaurants that are in neither Manhattan nor Brooklyn, using $nor. medium restaurants
  4. Find restaurants whose price is NOT greater than 2, using $not. medium restaurants
  5. Find restaurants that are open (isOpen true) AND are either Indian or Italian. medium restaurants
  6. Find restaurants that are NOT American and NOT Pizza, OR whose name starts with the letter B. hard restaurants
  7. Find movies rated R that belong to either the Action or Sci-Fi genre. hard movies
  8. Find restaurants that are closed (isOpen false) 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

  • $nor and $not match documents where the field is absent.
  • Mixing: put $or inside $and when you need "X and (Y or Z)".

Common questions

When do I actually need $and in MongoDB?

Only when you need the same field twice, like { $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