0
0
MongoDBquery~10 mins

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

Choose your learning style9 modes available
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.