The $nor operator helps find documents that do not match any of the given conditions. It is like saying "none of these" in a search.
$nor operator behavior in MongoDB
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
MongoDB
{ $nor: [ { condition1 }, { condition2 }, ... ] }The $nor operator takes an array of conditions.
It returns documents where none of the conditions are true.
Examples
age is neither less than 18 nor greater than 65.MongoDB
{ $nor: [ { age: { $lt: 18 } }, { age: { $gt: 65 } } ] }status is not "A" and qty is not less than 30.MongoDB
{ $nor: [ { status: "A" }, { qty: { $lt: 30 } } ] }Sample Program
This query finds documents where status is not "A" and qty is not less than 50. It excludes any document matching either condition.
MongoDB
db.inventory.insertMany([
{ item: "journal", qty: 25, status: "A" },
{ item: "notebook", qty: 50, status: "A" },
{ item: "paper", qty: 100, status: "D" },
{ item: "planner", qty: 75, status: "D" },
{ item: "postcard", qty: 45, status: "A" }
])
// Find items where status is neither "A" nor qty less than 50
const result = db.inventory.find({ $nor: [ { status: "A" }, { qty: { $lt: 50 } } ] }).toArray()
printjson(result)Important Notes
The $nor operator is the opposite of $or. It returns documents that do not satisfy any of the conditions.
If the array is empty, $nor matches all documents.
Summary
$nor finds documents where none of the given conditions are true.
It takes an array of conditions and excludes documents matching any of them.
Useful to filter out unwanted records based on multiple criteria.
Practice
1. What does the
$nor operator do in MongoDB queries?easy
Solution
Step 1: Understand the purpose of
The$nor$noroperator returns documents that do not satisfy any of the given conditions.Step 2: Compare with other logical operators
Unlike$andor$or,$norexcludes documents matching any condition in its array.Final Answer:
Finds documents where none of the specified conditions are true -> Option CQuick Check:
$norexcludes all matching conditions = B [OK]
Hint: Think: no conditions should be true for
$nor [OK]Common Mistakes:
- Confusing
$norwith$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
Solution
Step 1: Recall
$norsyntax$norrequires an array of condition objects inside square brackets.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.Final Answer:
{ $nor: [ { age: 25 }, { status: "active" } ] } -> Option DQuick 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
$orinside$norunnecessarily
3. Given the collection documents:
What will be the result of this query?
[{ "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
Solution
Step 1: Understand the
The query excludes documents where$norconditionsageis 25 ORstatusis "active".Step 2: Check each document against conditions
Alice: status "active" -> excluded; Bob: age 25 -> excluded; Carol: status "active" -> excluded.Final Answer:
[] (empty array) -> Option AQuick 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
$norwith$or - Assuming some documents pass when all match conditions
4. You wrote this MongoDB query but it throws an error:
What is the problem and how to fix it?
{ $nor: { age: { $gt: 30 }, status: "inactive" } }What is the problem and how to fix it?
medium
Solution
Step 1: Identify the syntax error
$norexpects an array of condition objects, but here it has a single object.Step 2: Correct the syntax
Wrap each condition inside its own object within an array to fix the error.Final Answer:
The conditions must be inside an array; fix: { $nor: [ { age: { $gt: 30 } }, { status: "inactive" } ] } -> Option BQuick Check:
$norneeds 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
$norwith$and
5. You have a collection with documents:
Write a
[{ "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
Solution
Step 1: Understand the requirement
We want documents whereqtyis NOT 0 andstatusis NOT "D".Step 2: Use
{ $nor: [ { qty: 0 }, { status: "D" } ] } excludes documents with$norto exclude documents withqty0 orstatus"D"qty0 orstatus"D", so it returns documents matching the requirement.Final Answer:
{ $nor: [ { qty: 0 }, { status: "D" } ] } -> Option AQuick 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
