Challenge - 5 Problems
MongoDB $not Operator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of $not with $gt operator
Given a collection
What documents are returned by this query?
products with documents:{ "name": "A", "price": 50 }{ "name": "B", "price": 100 }{ "name": "C", "price": 150 }What documents are returned by this query?
{ price: { $not: { $gt: 100 } } }Attempts:
2 left
💡 Hint
Remember $not inverts the condition inside it.
✗ Incorrect
The query { price: { $not: { $gt: 100 } } } selects documents where price is NOT greater than 100. So prices 100 and below match.
🧠 Conceptual
intermediate2:00remaining
Understanding $not with regex
What does the following MongoDB query return?
Assuming documents have a
{ name: { $not: /abc/ } }Assuming documents have a
name field with string values.Attempts:
2 left
💡 Hint
Think about what $not does to the regex match.
✗ Incorrect
The $not operator negates the regex match. So it returns documents where name does NOT match the regex /abc/.
📝 Syntax
advanced2:00remaining
Identify syntax error with $not usage
Which of the following MongoDB queries will cause a syntax error?
Attempts:
2 left
💡 Hint
Check the allowed value types for $not operator.
✗ Incorrect
The $not operator requires a query expression or regex inside it. A direct value like 18 is invalid syntax.
❓ optimization
advanced2:00remaining
Optimizing $not queries
Which query is more efficient to find documents where
score is NOT greater than 50?Attempts:
2 left
💡 Hint
Think about how indexes and query operators work.
✗ Incorrect
Using $lte directly is more efficient because it can use indexes better than $not or $nor which invert conditions.
🔧 Debug
expert3:00remaining
Debugging unexpected $not behavior
A developer runs this query:
But it returns documents where
Why does this happen?
{ status: { $not: { $eq: "active" } } }But it returns documents where
status is missing or null, which was not expected.Why does this happen?
Attempts:
2 left
💡 Hint
Consider how $not treats documents missing the field.
✗ Incorrect
The $not operator matches documents where the condition inside is false or the field is missing or null. So documents without status also match.