Bird
Raised Fist0
MongoDBquery~10 mins

$nor operator behavior in MongoDB - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - $nor operator behavior
Start with $nor query
Evaluate each condition inside $nor
Are ALL conditions false?
NoDocument excluded
Yes
Document included in result
The $nor operator checks multiple conditions and returns documents only if all those conditions are false.
Execution Sample
MongoDB
db.collection.find({ $nor: [ { age: { $lt: 30 } }, { city: 'NY' } ] })
Find documents where age is NOT less than 30 AND city is NOT 'NY'.
Execution Table
StepDocumentCondition 1 (age < 30)Condition 2 (city = 'NY')All conditions false?Include Document?
1{ age: 25, city: 'LA' }TrueFalseNoNo
2{ age: 35, city: 'NY' }FalseTrueNoNo
3{ age: 40, city: 'LA' }FalseFalseYesYes
4{ age: 28, city: 'NY' }TrueTrueNoNo
5{ age: 50, city: 'SF' }FalseFalseYesYes
6End of documentsQuery ends
💡 Query ends after checking all documents; only those with all conditions false are included.
Variable Tracker
DocumentCondition 1 (age < 30)Condition 2 (city = 'NY')All conditions false?Included
{ age: 25, city: 'LA' }TrueFalseNoNo
{ age: 35, city: 'NY' }FalseTrueNoNo
{ age: 40, city: 'LA' }FalseFalseYesYes
{ age: 28, city: 'NY' }TrueTrueNoNo
{ age: 50, city: 'SF' }FalseFalseYesYes
Key Moments - 2 Insights
Why does a document get excluded if only one condition inside $nor is true?
Because $nor requires ALL conditions to be false to include the document. If even one condition is true, the document is excluded, as shown in rows 1 and 2 of the execution_table.
Is $nor the same as negating $or?
Yes, $nor returns documents where none of the $or conditions are true. This means all conditions inside $nor must be false, as demonstrated in the execution_table where documents with any true condition are excluded.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'All conditions false?' for the document { age: 40, city: 'LA' }?
ATrue
BYes
CNo
DFalse
💡 Hint
Check the row where Document is { age: 40, city: 'LA' } in the execution_table under 'All conditions false?' column.
At which step does the query include the document in the result?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look for the row where 'Include Document?' is 'Yes' in the execution_table.
If the condition { city: 'NY' } was removed from $nor, how would the inclusion of documents change?
AAll documents would be included
BNo documents would be included
COnly documents with age >= 30 would be included
DOnly documents with city 'NY' would be included
💡 Hint
Removing one condition means only the remaining condition affects inclusion; check how condition 1 affects inclusion in variable_tracker.
Concept Snapshot
$nor operator:
- Takes an array of conditions.
- Returns documents where ALL conditions are false.
- Equivalent to NOT (condition1 OR condition2 ...).
- Useful to exclude multiple conditions at once.
- Syntax: { $nor: [ {cond1}, {cond2}, ... ] }
Full Transcript
The $nor operator in MongoDB is used to find documents where none of the specified conditions are true. It evaluates each condition inside its array and includes only those documents where all conditions are false. For example, if you query with $nor: [ { age: { $lt: 30 } }, { city: 'NY' } ], it returns documents where age is not less than 30 and city is not 'NY'. The execution table shows step-by-step how each document is checked against the conditions, and only those with all false conditions are included. This operator is like negating an OR condition, so if any condition is true, the document is excluded. Understanding this helps avoid confusion about why some documents are included or excluded in query results.

Practice

(1/5)
1. What does the $nor operator do in MongoDB queries?
easy
A. Finds documents where all specified conditions are true
B. Finds documents where at least one condition is true
C. Finds documents where none of the specified conditions are true
D. Finds documents that match exactly one condition

Solution

  1. Step 1: Understand the purpose of $nor

    The $nor operator returns documents that do not satisfy any of the given conditions.
  2. Step 2: Compare with other logical operators

    Unlike $and or $or, $nor excludes documents matching any condition in its array.
  3. Final Answer:

    Finds documents where none of the specified conditions are true -> Option C
  4. Quick Check:

    $nor excludes all matching conditions = B [OK]
Hint: Think: no conditions should be true for $nor [OK]
Common Mistakes:
  • Confusing $nor with $or
  • Assuming it returns documents matching any condition
  • Thinking it requires all conditions to be true
2. Which of the following is the correct syntax to use $nor in a MongoDB query to exclude documents where age is 25 or status is "active"?
easy
A. { $nor: { $or: [ { age: 25 }, { status: "active" } ] } }
B. { $nor: { age: 25, status: "active" } }
C. { $nor: [ age: 25, status: "active" ] }
D. { $nor: [ { age: 25 }, { status: "active" } ] }

Solution

  1. Step 1: Recall $nor syntax

    $nor requires an array of condition objects inside square brackets.
  2. Step 2: Check each option's structure

    { $nor: [ { age: 25 }, { status: "active" } ] } correctly uses an array of conditions. Options B and D use objects incorrectly, and C has invalid array syntax.
  3. Final Answer:

    { $nor: [ { age: 25 }, { status: "active" } ] } -> Option D
  4. Quick Check:

    Array of conditions inside $nor = A [OK]
Hint: Use square brackets for conditions array in $nor [OK]
Common Mistakes:
  • Using curly braces instead of square brackets for conditions
  • Passing a single object instead of an array
  • Nesting $or inside $nor unnecessarily
3. Given the collection documents:
[{ "name": "Alice", "age": 30, "status": "active" }, { "name": "Bob", "age": 25, "status": "inactive" }, { "name": "Carol", "age": 35, "status": "active" }]

What will be the result of this query?
{ $nor: [ { age: 25 }, { status: "active" } ] }
medium
A. [] (empty array)
B. [{ "name": "Bob", "age": 25, "status": "inactive" }]
C. [{ "name": "Bob", "age": 25, "status": "inactive" }, { "name": "Carol", "age": 35, "status": "active" }]
D. [{ "name": "Bob", "age": 25, "status": "inactive" }, { "name": "Alice", "age": 30, "status": "active" }]

Solution

  1. Step 1: Understand the $nor conditions

    The query excludes documents where age is 25 OR status is "active".
  2. Step 2: Check each document against conditions

    Alice: status "active" -> excluded; Bob: age 25 -> excluded; Carol: status "active" -> excluded.
  3. Final Answer:

    [] (empty array) -> Option A
  4. Quick Check:

    All documents match at least one condition, so none returned = A [OK]
Hint: Exclude any document matching any condition in $nor [OK]
Common Mistakes:
  • Returning documents that match one condition
  • Confusing $nor with $or
  • Assuming some documents pass when all match conditions
4. You wrote this MongoDB query but it throws an error:
{ $nor: { age: { $gt: 30 }, status: "inactive" } }

What is the problem and how to fix it?
medium
A. The field names must be strings; fix: { $nor: [ { "age": { $gt: 30 } }, { "status": "inactive" } ] }
B. The conditions must be inside an array; fix: { $nor: [ { age: { $gt: 30 } }, { status: "inactive" } ] }
C. The operator $gt is invalid; fix: use $gte instead
D. The query must use $and instead of $nor

Solution

  1. Step 1: Identify the syntax error

    $nor expects an array of condition objects, but here it has a single object.
  2. Step 2: Correct the syntax

    Wrap each condition inside its own object within an array to fix the error.
  3. Final Answer:

    The conditions must be inside an array; fix: { $nor: [ { age: { $gt: 30 } }, { status: "inactive" } ] } -> Option B
  4. Quick Check:

    $nor needs array of conditions = C [OK]
Hint: Always use an array of conditions with $nor [OK]
Common Mistakes:
  • Passing a single object instead of an array
  • Misusing comparison operators
  • Confusing $nor with $and
5. You have a collection with documents:
[{ "item": "pen", "qty": 10, "status": "A" }, { "item": "pencil", "qty": 5, "status": "D" }, { "item": "notebook", "qty": 15, "status": "A" }, { "item": "eraser", "qty": 0, "status": "D" }]

Write a $nor query to find documents where qty is not 0 and status is not "D". Which query returns the correct documents?
hard
A. { $nor: [ { qty: 0 }, { status: "D" } ] }
B. { $nor: [ { qty: { $ne: 0 } }, { status: { $ne: "D" } } ] }
C. { $nor: [ { qty: { $eq: 0 } }, { status: { $ne: "D" } } ] }
D. { $nor: [ { qty: { $gt: 0 } }, { status: { $ne: "D" } } ] }

Solution

  1. Step 1: Understand the requirement

    We want documents where qty is NOT 0 and status is NOT "D".
  2. Step 2: Use $nor to exclude documents with qty 0 or status "D"

    { $nor: [ { qty: 0 }, { status: "D" } ] } excludes documents with qty 0 or status "D", so it returns documents matching the requirement.
  3. Final Answer:

    { $nor: [ { qty: 0 }, { status: "D" } ] } -> Option A
  4. Quick Check:

    Exclude unwanted values with $nor = D [OK]
Hint: Use $nor to exclude unwanted values directly [OK]
Common Mistakes:
  • Using $ne inside $nor incorrectly
  • Confusing inclusion with exclusion logic
  • Using wrong comparison operators inside conditions