Bird
Raised Fist0
MongoDBquery~10 mins

$not operator behavior in MongoDB - Step-by-Step Execution

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
Concept Flow - $not operator behavior
Start with a query condition
Apply $not operator
Invert the condition result
Return documents where condition is false
End
The $not operator in MongoDB inverts the result of a query condition, returning documents where the condition is false.
Execution Sample
MongoDB
{ age: { $not: { $gt: 30 } } }
Find documents where age is NOT greater than 30.
Execution Table
StepDocumentCondition (age > 30)Apply $notResult (matches query)
1{ age: 25 }25 > 30 = falsenot false = truetrue (included)
2{ age: 30 }30 > 30 = falsenot false = truetrue (included)
3{ age: 35 }35 > 30 = truenot true = falsefalse (excluded)
4{ age: 40 }40 > 30 = truenot true = falsefalse (excluded)
💡 All documents checked; only those where age > 30 is false are included.
Variable Tracker
VariableStartAfter Doc 1After Doc 2After Doc 3After Doc 4Final
ConditionResultN/AfalsefalsetruetrueN/A
NotResultN/AtruetruefalsefalseN/A
Key Moments - 2 Insights
Why does $not return documents where the condition is false, not true?
Because $not inverts the condition result. For example, in row 1 of execution_table, age > 30 is false, so $not makes it true, including the document.
Does $not work alone without another operator?
No, $not must be used with another operator like $gt, $eq, etc. It negates that operator's condition as shown in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the $not result for document { age: 30 } at step 2?
Afalse
Btrue
Cundefined
Dnull
💡 Hint
Check the 'Apply $not' column in row 2 of the execution_table.
At which step does the document get excluded from the result?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Result (matches query)' column where it says 'false (excluded)'.
If the condition was age < 30 instead of age > 30, which document would be excluded?
A{ age: 35 }
B{ age: 30 }
C{ age: 25 }
D{ age: 40 }
💡 Hint
Think about which ages are less than 30 and how $not inverts that.
Concept Snapshot
$not operator in MongoDB:
- Used to invert a query condition.
- Syntax: { field: { $not: { <operator>: <value> } } }
- Returns documents where the condition is false.
- Must be used with another operator (e.g., $gt, $eq).
- Useful to exclude documents matching a condition.
Full Transcript
The $not operator in MongoDB reverses the result of a query condition. For example, if you want to find documents where age is NOT greater than 30, you write { age: { $not: { $gt: 30 } } }. The execution table shows step-by-step how each document is checked: the condition age > 30 is evaluated, then $not flips the result. Documents where the condition is false become true after $not, so they are included in the results. This operator must be used with another operator and cannot stand alone. Understanding this helps you filter data by excluding certain conditions.

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

  1. Step 1: Understand the purpose of $not

    The $not operator reverses the condition it wraps, so it matches documents where the condition is false.
  2. 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.
  3. Final Answer:

    It selects documents where the condition inside $not is false. -> Option A
  4. 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

  1. Step 1: Review correct $not syntax

    The $not operator must wrap another operator inside the field, like { field: { $not: { $gt: 10 } } }.
  2. 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.
  3. Final Answer:

    { field: { $not: { $gt: 10 } } } -> Option A
  4. Quick Check:

    Correct $not syntax wraps operator inside field [OK]
Hint: Wrap operator inside $not within the field object [OK]
Common Mistakes:
  • Placing $not outside the field
  • Not wrapping the operator inside $not
  • Using invalid JSON structure with $not
3. Given the collection documents:
[{ "score": 5 }, { "score": 10 }, { "score": 15 }]
What will be the result of this query?
{ "score": { "$not": { "$gt": 10 } } }
medium
A. []
B. [{ "score": 15 }]
C. [{ "score": 5 }]
D. [{ "score": 5 }, { "score": 10 }]

Solution

  1. Step 1: Understand the query condition

    The query uses $not with $gt: 10, so it matches documents where score is NOT greater than 10.
  2. Step 2: Check each document against the condition

    Documents with scores 5 and 10 are not greater than 10, so they match. The document with 15 does not match.
  3. Final Answer:

    [{ "score": 5 }, { "score": 10 }] -> Option D
  4. Quick Check:

    Scores ≤ 10 match $not $gt 10 [OK]
Hint: Think: $not $gt 10 means ≤ 10 [OK]
Common Mistakes:
  • Selecting documents where score > 10 instead
  • Confusing $not with $ne
  • Ignoring the nested operator inside $not
4. You wrote this query but it returns an error:
{ "name": { "$not": "^A" } }

What is the problem?
medium
A. The query is missing a closing brace.
B. $not must be used with another operator like $regex.
C. $not cannot be used on string fields.
D. The regex pattern is invalid without slashes.

Solution

  1. Step 1: Analyze the use of $not with a string

    The $not operator expects an operator expression, not a direct string.
  2. Step 2: Correct usage with regex

    To negate a regex, you must use { "$not": { "$regex": "^A" } }. The given query misses $regex.
  3. Final Answer:

    $not must be used with another operator like $regex. -> Option B
  4. Quick Check:

    $not needs operator, not raw value [OK]
Hint: Always pair $not with an operator like $regex [OK]
Common Mistakes:
  • Using raw string inside $not without operator
  • Assuming regex needs slashes in MongoDB
  • Thinking $not works on any value directly
5. You want to find documents where the status field does NOT start with the letter 'P'. Which query correctly uses $not with a regex to achieve this?
hard
A. { "status": { "$regex": { "$not": "^P" } } }
B. { "$not": { "status": $regex: "^P" } }
C. { "status": { "$not": { "$regex": "^P" } } }
D. { "status": { "$not": "^P" } }

Solution

  1. Step 1: Understand the goal

    You want documents where status does NOT start with 'P', so negate the regex ^P.
  2. Step 2: Use $not with $regex inside the field

    The correct syntax is { "status": { "$not": { "$regex": "^P" } } }. This matches documents where status does not match the regex.
  3. Final Answer:

    { "status": { "$not": { "$regex": "^P" } } } -> Option C
  4. Quick Check:

    $not wraps $regex inside field [OK]
Hint: Wrap regex inside $not within the field object [OK]
Common Mistakes:
  • Placing $not outside the field
  • Using $not directly on string without $regex
  • Incorrect nesting of $not and $regex