The $not operator helps you find data that does NOT match a certain condition. It flips the rule you give it.
$not operator behavior in MongoDB
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
MongoDB
{ field: { $not: { <operator-expression> } } }The $not operator must be used inside a field condition.
The <operator-expression> is usually another operator like $lt, $gt, or a regex.
Examples
age is NOT less than 30 (age >= 30).MongoDB
{ age: { $not: { $lt: 30 } } }name does NOT contain 'John'.MongoDB
{ name: { $not: /John/ } }score is NOT greater than or equal to 50 (score < 50).MongoDB
{ score: { $not: { $gte: 50 } } }Sample Program
This inserts four students with different ages. Then it finds students whose age is NOT less than 30, meaning age is 30 or more.
MongoDB
db.students.insertMany([
{ name: "Alice", age: 25 },
{ name: "Bob", age: 35 },
{ name: "Charlie", age: 30 },
{ name: "David", age: 28 }
])
// Find students NOT younger than 30
const result = db.students.find({ age: { $not: { $lt: 30 } } }).toArray()
resultImportant Notes
$not works only with other operators or regex inside it, not with direct values.
It reverses the meaning of the condition inside it.
Remember to use $not inside a field condition, not as a top-level operator.
Summary
$not flips the condition you give it to find data that does NOT match.
Use it inside a field with another operator or regex.
It helps exclude unwanted data easily.
Practice
1. What does the
$not operator do in MongoDB queries?easy
Solution
Step 1: Understand the purpose of
The$not$notoperator 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$notis false. correctly states that$notselects documents where the condition inside it is false, which is the correct behavior.Final Answer:
It selects documents where the condition inside$notis false. -> Option AQuick Check:
$notflips condition = false [OK]
Hint: Remember:
$not means 'not matching' condition [OK]Common Mistakes:
- Thinking
$notselects where condition is true - Confusing
$notwith delete or update operations - Using
$notwithout a condition inside
2. Which of the following is the correct syntax to use
$not with a comparison operator in MongoDB?easy
Solution
Step 1: Review correct
The$notsyntax$notoperator 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 AQuick Check:
Correct$notsyntax wraps operator inside field [OK]
Hint: Wrap operator inside
$not within the field object [OK]Common Mistakes:
- Placing
$notoutside 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
Solution
Step 1: Understand the query condition
The query uses$notwith$gt: 10, so it matches documents where score is NOT greater than 10.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.Final Answer:
[{ "score": 5 }, { "score": 10 }] -> Option DQuick 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
$notwith$ne - Ignoring the nested operator inside
$not
4. You wrote this query but it returns an error:
What is the problem?
{ "name": { "$not": "^A" } }What is the problem?
medium
Solution
Step 1: Analyze the use of
The$notwith a string$notoperator expects an operator expression, not a direct string.Step 2: Correct usage with regex
To negate a regex, you must use{ "$not": { "$regex": "^A" } }. The given query misses$regex.Final Answer:
$notmust be used with another operator like$regex. -> Option BQuick Check:
$notneeds operator, not raw value [OK]
Hint: Always pair
$not with an operator like $regex [OK]Common Mistakes:
- Using raw string inside
$notwithout operator - Assuming regex needs slashes in MongoDB
- Thinking
$notworks 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
Solution
Step 1: Understand the goal
You want documents wherestatusdoes NOT start with 'P', so negate the regex^P.Step 2: Use
The correct syntax is$notwith$regexinside the field{ "status": { "$not": { "$regex": "^P" } } }. This matches documents wherestatusdoes not match the regex.Final Answer:
{ "status": { "$not": { "$regex": "^P" } } } -> Option CQuick Check:
$notwraps$regexinside field [OK]
Hint: Wrap regex inside
$not within the field object [OK]Common Mistakes:
- Placing
$notoutside the field - Using
$notdirectly on string without$regex - Incorrect nesting of
$notand$regex
