The $not operator negates a condition. In queries it inverts an operator on a field (e.g., “not less than 10”). In aggregation expressions it flips a boolean result from true to false.
01
Negation
Invert a condition.
02
Two Forms
Query vs expression.
03
Field-Level
Negate $lt, $gt, etc.
04
$expr / $project
Boolean NOT logic.
05
Use Cases
Ranges, regex, flags.
06
vs $ne / $nor
Know the difference.
Fundamentals
Definition and Usage
In MongoDB, $not performs logical negation in two contexts. As a query operator, it inverts a single operator on a field: { price: { $not: { $lt: 10 } } } matches documents where price is not less than 10 (equivalent to { price: { $gte: 10 } } for numeric fields).
As an aggregation expression, { $not: <expression> } returns true when the inner expression is false, and vice versa. Wrap $or, $and, or comparison expressions inside $not for complex inverted logic.
💡
Beginner Tip
In query form, $not must be applied to a field — you cannot use it as a top-level filter by itself. For excluding a plain value, $ne is simpler. Use $not when negating operators like $lt, $gt, or $regex.
Foundation
📝 Syntax
$not has two forms depending on where you use it:
Query form (negate an operator on a field)
mongosh
{ field: { $not: <operator expression> } }
Aggregation expression form (negate a boolean)
mongosh
{ $not: <boolean expression> }
Syntax Rules
Query form — negates one operator expression on a specific field.
Expression form — inverts a boolean result; use in $project, $addFields, or $expr.
Query $not also matches documents where the field is missing or null.
Cannot use query $not at the top level of a filter (must be on a field).
For simple value inequality, prefer $ne over $not.
Wrap $or inside expression $not to replicate $nor logic.
In aggregation mode, $not wraps a boolean expression. $lt returns true when price < 20; $not flips that to false for discounted items.
Example 4 — $not with $or Inside $expr
Replicate $nor logic using expression $not + $or:
mongosh
db.products.find({
$expr: {
$not: {
$or: [
{ $eq: [ "$inStock", false ] },
{ $lt: [ "$price", 10 ] }
]
}
}
})
// Matches products that are NOT (out of stock OR under $10)
How It Works
When you need NOR-style logic with field comparisons, wrap an $or inside expression $not within $expr. This is the aggregation equivalent of query $nor.
Bonus — $not in $match Stage
Filter adults (age not less than 18) at the start of a pipeline:
Query $not works directly in $match without $expr when negating a standard field operator. Users under 18 are filtered out before later stages run.
Applications
🚀 Use Cases
Inverted comparisons — express “not less than” or “not greater than” on numeric fields.
Regex exclusion — exclude documents matching a text pattern.
Boolean flags — flip true/false in $project for reporting.
NOR in expressions — wrap $or with $not inside $expr.
🧠 How $not Works
1
MongoDB evaluates the inner expression
In queries, a field operator like $lt is tested. In expressions, a boolean sub-expression is evaluated.
Input
2
$not inverts the result
True becomes false, false becomes true. In query mode, documents matching the inner condition are excluded.
Negate
3
Inverted result is applied
In $match, non-matching documents pass. In $project, the flipped boolean is stored.
Output
=
🔄
Negated conditions
You get filters or flags with inverted logic for ranges, patterns, and booleans.
Wrap Up
Conclusion
The $not operator negates conditions in MongoDB queries and aggregation expressions. Use query $not to invert field operators like $lt and $regex, and expression $not to flip boolean results in pipelines.
For simple value inequality, $ne is clearer. For multiple independent exclusions, use $nor. Next in the series: $objectToArray.
Use query $not to negate operators like $lt, $gt, $regex
Use expression $not in $project for inverted boolean flags
Prefer direct operators when clearer ($gte over $not: { $lt })
Wrap $or with $not in $expr for NOR logic
Remember query $not includes missing/null fields
❌ Don’t
Use $not at the top level of a query (must be on a field)
Choose $not over $ne for simple value inequality
Confuse query $not syntax with expression $not syntax
Assume $not: { $lt: 10 } } and $gte: 10 behave identically on null/missing in all edge cases without testing
Over-nest $not when $nor or $nin is clearer
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $not
Use these points when negating conditions in MongoDB.
5
Core concepts
🔄01
Negation
Inverts a condition.
Purpose
📝02
Two Forms
Query vs expression.
Syntax
🔍03
Field-Level
Negate $lt, $regex.
Query
⚙04
Boolean Flip
In $project/$expr.
Expression
📑05
Not $ne
Different use cases.
Related
❓ Frequently Asked Questions
$not negates a condition. In queries it inverts an operator on a field: { price: { $not: { $lt: 10 } } } matches prices that are not less than 10. In aggregation expressions it returns true when the inner expression is false.
Query form: { field: { $not: <operator expression> } }. Aggregation expression form: { $not: <boolean expression> }. The query form negates one field condition; the expression form negates a boolean result.
$ne compares a field to a single value: { status: { $ne: "archived" } }. $not negates an operator expression: { price: { $not: { $lt: 10 } } }. Use $not when inverting comparison operators like $lt, $gt, or $regex.
$not negates one condition (on one field in query form). $nor takes an array of separate conditions and matches when none of them match. Use $nor for multiple independent exclusions.
Yes, as an aggregation expression: { $not: <expression> } returns the logical inverse of a boolean expression. You can wrap $or, $and, $lt, and other expression operators inside $not.