The $not operator helps you find data that does NOT match a certain condition. It flips the rule you give it.
You want to find all users who do NOT live in a specific city.
You want to get products that do NOT have a price less than 100.
You want to find orders that do NOT have a status of 'shipped'.
You want to exclude documents where a field matches a certain pattern.
You want to filter out records that do NOT meet a numeric condition.
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
Find documents where age is NOT less than 30 (age >= 30).
MongoDB
{ age: { $not: { $lt: 30 } } }
Find documents where name does NOT contain 'John'.
MongoDB
{ name: { $not: /John/ } }
Find documents where 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 30const result = db.students.find({ age: { $not: { $lt: 30 } } }).toArray()
result
OutputSuccess
Important 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/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]