MongoDB $not Operator

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 4 Examples
Logical

What You’ll Learn

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.

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.

📝 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.

💡 $not vs $ne vs $nor vs $nin

$ne{ status: { $ne: "archived" } } (field ≠ one value)
$not{ price: { $not: { $lt: 10 } } } (negate an operator)
$nor{ $nor: [ { a: 1 }, { b: 2 } ] } (none of several conditions)
$nin{ status: { $nin: ["a", "b"] } } (field not in value list)

⚡ Quick Reference

QuestionAnswer
Operator typeLogical negation (query + aggregation expression)
Query syntax{ field: { $not: { $op: value } } }
Expression syntax{ $not: <boolean expr> }
Expression outputInverted boolean (true ↔ false)
Common stagesfind(), $match, $project, $expr
Not less than 10
{
  price: { $not: { $lt: 10 } }
}

Same as $gte: 10

Not a regex
{
  name: {
    $not: { $regex: /^test/ }
  }
}

Exclude pattern

In $project
{
  $not: {
    $lt: [ "$price", 20 ]
  }
}

Boolean flip

Negate $or
{
  $not: {
    $or: [ expr1, expr2 ]
  }
}

NOR in $expr

Examples Gallery

Negate price comparisons in queries, filter with regex exclusion, flip booleans in $project, and build NOR logic in $expr.

📚 Price Filter

Find products whose price is not less than $10 using query $not.

Sample Input Documents

Suppose you have a products collection:

mongosh
[
  { "_id": ObjectId("609c26812e9274a86871bc6a"), "name": "Widget",  "price": 25 },
  { "_id": ObjectId("609c26812e9274a86871bc6b"), "name": "Budget",  "price": 5 },
  { "_id": ObjectId("609c26812e9274a86871bc6c"), "name": "Premium", "price": 99 },
  { "_id": ObjectId("609c26812e9274a86871bc6d"), "name": "Mid",     "price": 10 }
]

Example 1 — Query $not on a Field

Find products where price is not less than 10:

mongosh
db.products.find({
  price: { $not: { $lt: 10 } }
})

// Equivalent to:
// db.products.find({ price: { $gte: 10 } })

How It Works

  • Budget (price 5) — matches $lt: 10, so $not excludes it.
  • Mid (price 10) — does not match $lt: 10, so $not includes it.
  • For many comparisons, the direct operator ($gte) reads more clearly than $not.

📈 Practical Patterns

Regex exclusion, boolean flags in pipelines, and NOR logic with expression $not.

Example 2 — Exclude Names Matching a Regex

Find users whose name does not start with “test”:

mongosh
db.users.find({
  name: {
    $not: { $regex: /^test/i }
  }
})

How It Works

Query $not with $regex is a common pattern for excluding pattern matches. This is harder to express with $ne alone.

Example 3 — $not in $project (Aggregation Expression)

Add a flag for products that are not discounted (price not below $20):

mongosh
db.products.aggregate([
  {
    $project: {
      name: 1,
      price: 1,
      isFullPrice: {
        $not: { $lt: [ "$price", 20 ] }
      }
    }
  }
])

How It Works

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:

mongosh
db.users.aggregate([
  {
    $match: {
      age: { $not: { $lt: 18 } }
    }
  },
  {
    $project: {
      name: 1,
      age: 1
    }
  }
])

How It Works

Query $not works directly in $match without $expr when negating a standard field operator. Users under 18 are filtered out before later stages run.

🚀 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.

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.

💡 Best Practices

✅ Do

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about $not

Use these points when negating conditions in MongoDB.

5
Core concepts
📝 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.

Continue the Operator Series

Move on to $objectToArray for object conversion, or review $ne for value inequality.

Next: $objectToArray →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful