0
0
MongoDBquery~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB $not Operator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of $not with $gt operator
Given a collection 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 } } }
A[{ "name": "A", "price": 50 }, { "name": "B", "price": 100 }]
B[{ "name": "C", "price": 150 }]
C[{ "name": "A", "price": 50 }]
D[{ "name": "B", "price": 100 }, { "name": "C", "price": 150 }]
Attempts:
2 left
💡 Hint
Remember $not inverts the condition inside it.
🧠 Conceptual
intermediate
2:00remaining
Understanding $not with regex
What does the following MongoDB query return?
{ name: { $not: /abc/ } }

Assuming documents have a name field with string values.
ADocuments where <code>name</code> contains the substring 'abc'.
BDocuments where <code>name</code> is missing or null.
CDocuments where <code>name</code> is exactly 'abc'.
DDocuments where <code>name</code> does NOT contain the substring 'abc'.
Attempts:
2 left
💡 Hint
Think about what $not does to the regex match.
📝 Syntax
advanced
2:00remaining
Identify syntax error with $not usage
Which of the following MongoDB queries will cause a syntax error?
A{ age: { $not: { $exists: true } } }
B{ age: { $not: { $gte: 18 } } }
C{ age: { $not: 18 } }
D{ age: { $not: { $in: [18, 21] } } }
Attempts:
2 left
💡 Hint
Check the allowed value types for $not operator.
optimization
advanced
2:00remaining
Optimizing $not queries
Which query is more efficient to find documents where score is NOT greater than 50?
A{ score: { $lte: 50 } }
B{ score: { $not: { $gt: 50 } } }
C{ $nor: [ { score: { $gt: 50 } } ] }
D{ score: { $lt: 51 } }
Attempts:
2 left
💡 Hint
Think about how indexes and query operators work.
🔧 Debug
expert
3:00remaining
Debugging unexpected $not behavior
A developer runs this query:
{ status: { $not: { $eq: "active" } } }

But it returns documents where status is missing or null, which was not expected.
Why does this happen?
ABecause $eq inside $not is invalid and causes unexpected results.
BBecause $not matches documents where the field does not exist or is null in addition to not matching the condition.
CBecause $not only works with regex, not with $eq operator.
DBecause the query syntax is incorrect and MongoDB ignores the $not operator.
Attempts:
2 left
💡 Hint
Consider how $not treats documents missing the field.