Bird
Raised Fist0
MongoDBquery~20 mins

$nor operator behavior in MongoDB - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
MongoDB $nor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What documents are returned by this $nor query?
Given a collection products with documents:
{"item": "apple", "price": 5, "stock": 10}
{"item": "banana", "price": 3, "stock": 0}
{"item": "orange", "price": 7, "stock": 5}

What documents will be returned by this query?
{ "$nor": [ { "price": { "$lt": 4 } }, { "stock": { "$eq": 0 } } ] }
A[{"item": "orange", "price": 7, "stock": 5}]
B[{"item": "banana", "price": 3, "stock": 0}]
C[{"item": "apple", "price": 5, "stock": 10}, {"item": "orange", "price": 7, "stock": 5}]
D[{"item": "apple", "price": 5, "stock": 10}]
Attempts:
2 left
💡 Hint
Remember $nor returns documents where none of the conditions inside it are true.
🧠 Conceptual
intermediate
1:30remaining
How does $nor differ from $not in MongoDB?
Which statement correctly describes the difference between $nor and $not operators in MongoDB?
A$nor takes an array of conditions and returns documents where none are true; $not negates a single condition.
B$nor only works with numeric fields; $not works with string fields.
C$nor and $not are interchangeable and behave the same way.
D$nor negates a single condition; $not takes an array of conditions and returns documents where none are true.
Attempts:
2 left
💡 Hint
Think about how many conditions each operator accepts and what they negate.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in this $nor query
Which option contains a syntax error in the $nor query?
MongoDB
db.collection.find({ $nor: { price: { $lt: 10 }, stock: 0 } })
Adb.collection.find({ $nor: [ { price: { $lt: 10 } }, { stock: { $ne: 0 } } ] })
Bdb.collection.find({ $nor: [ { price: { $lt: 10 } }, { stock: 0 } ] })
Cdb.collection.find({ $nor: [ { price: { $lt: 10 } }, { stock: { $eq: 0 } } ] })
Ddb.collection.find({ $nor: { price: { $lt: 10 }, stock: 0 } })
Attempts:
2 left
💡 Hint
$nor requires an array of conditions, not an object.
optimization
advanced
2:00remaining
Optimizing a $nor query for performance
You want to find documents where neither status is "active" nor age is less than 30. Which query is more efficient?
Adb.collection.find({ $and: [ { status: { $ne: "active" } }, { age: { $gte: 30 } } ] })
Bdb.collection.find({ status: { $ne: "active" }, age: { $gte: 30 } })
Cdb.collection.find({ $nor: [ { status: { $ne: "active" } }, { age: { $gte: 30 } } ] })
Ddb.collection.find({ $nor: [ { status: "active" }, { age: { $lt: 30 } } ] })
Attempts:
2 left
💡 Hint
Think about how $nor negates conditions and how $and with negations can be clearer.
🔧 Debug
expert
2:30remaining
Why does this $nor query return no documents?
Given documents:
{"name": "Alice", "score": 85}
{"name": "Bob", "score": 90}
{"name": "Carol", "score": 75}

Why does this query return an empty result?
{ "$nor": [ { "score": { "$gt": 70 } }, { "score": { "$lt": 90 } } ] }
ABecause $nor requires at least three conditions to work properly.
BBecause every document matches at least one condition inside $nor, so none are returned.
CBecause the query syntax is invalid and causes an error.
DBecause $nor only works with string fields, not numeric fields like score.
Attempts:
2 left
💡 Hint
Check if any document can avoid matching both conditions inside $nor.

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