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
Recall & Review
beginner
What does the $not operator do in MongoDB?
The $not operator in MongoDB inverts the condition it is applied to. It matches documents where the specified condition is not true.
Click to reveal answer
intermediate
How do you use $not with a regular expression in MongoDB?
You can use $not with a regular expression to find documents where a field does not match the pattern. For example: { field: { $not: /pattern/ } } finds documents where field does not match pattern.
Click to reveal answer
beginner
Can $not be used alone without another operator?
No. $not must be used with another operator or expression. It cannot be used alone because it needs a condition to invert.
Click to reveal answer
intermediate
What is the difference between $not and $ne in MongoDB?
$not inverts any condition, while $ne specifically means "not equal to" a value. $not can be used with other operators like $gt, $regex, etc., to negate them.
Click to reveal answer
beginner
Example: What does this query do? { age: { $not: { $gt: 30 } } }
This query finds documents where the age field is NOT greater than 30. So it matches documents where age is 30 or less, or where age does not exist.
Click to reveal answer
What does { field: { $not: { $eq: 5 } } } match?
ADocuments where field equals 5
BDocuments where field is less than 5
CDocuments where field is greater than 5
DDocuments where field does not equal 5
✗ Incorrect
$not inverts the $eq condition, so it matches documents where field is not equal to 5.
Can $not be used without another operator?
ANo, it requires another operator
BOnly with regular expressions
COnly with <code>$eq</code>
DYes, it works alone
✗ Incorrect
$not must be used with another operator or expression to invert its condition.
Which query finds documents where name does NOT start with 'A'?
A{ name: { $not: { $eq: 'A' } } }
B{ name: { $ne: /^A/ } }
C{ name: { $not: /^A/ } }
D{ name: { $regex: '^A' } }
✗ Incorrect
Using $not with a regex /^A/ matches documents where name does not start with 'A'.
What does { score: { $not: { $gt: 50 } } } select?
AScores greater than 50
BScores less than or equal to 50
CScores equal to 50 only
DScores not equal to 50
✗ Incorrect
It selects documents where score is NOT greater than 50, meaning 50 or less.
How does $not differ from $ne?
A<code>$not</code> negates any condition; <code>$ne</code> means not equal
BThey are the same
C<code>$ne</code> negates any condition; <code>$not</code> means not equal
DNeither negates conditions
✗ Incorrect
$not negates any operator condition; $ne specifically means 'not equal to' a value.
Explain how the $not operator works in MongoDB queries.
Think about how 'not' works in everyday language.
You got /3 concepts.
Describe a scenario where you would use $not with a regular expression in a MongoDB query.
Consider searching for things that do not match a pattern.
You got /3 concepts.
Practice
(1/5)
1. What does the $not operator do in MongoDB queries?
easy
A. It selects documents where the condition inside $not is false.
B. It selects documents where the condition inside $not is true.
C. It deletes documents that match the condition.
D. It updates documents that do not match the condition.
Solution
Step 1: Understand the purpose of $not
The $not operator reverses the condition it wraps, so it matches documents where the condition is false.
Step 2: Apply this understanding to the options
It selects documents where the condition inside $not is false. correctly states that $not selects documents where the condition inside it is false, which is the correct behavior.
Final Answer:
It selects documents where the condition inside $not is false. -> Option A
Quick Check:
$not flips condition = false [OK]
Hint: Remember: $not means 'not matching' condition [OK]
Common Mistakes:
Thinking $not selects where condition is true
Confusing $not with delete or update operations
Using $not without a condition inside
2. Which of the following is the correct syntax to use $not with a comparison operator in MongoDB?
easy
A. { field: { $not: { $gt: 10 } } }
B. { field: { $gt: { $not: 10 } } }
C. { $not: { field: $gt: 10 } }
D. { field: { $not: $gt: 10 } }
Solution
Step 1: Review correct $not syntax
The $not operator must wrap another operator inside the field, like { field: { $not: { $gt: 10 } } }.
Step 2: Check each option
{ field: { $not: { $gt: 10 } } } matches the correct syntax. Options A, B, and D misuse the placement or structure of $not.
Final Answer:
{ field: { $not: { $gt: 10 } } } -> Option A
Quick Check:
Correct $not syntax wraps operator inside field [OK]
Hint: Wrap operator inside $not within the field object [OK]