MongoDB $not Operator

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

What Is $not?

$not means logical NOT. In a query it inverts an operator on a field. In aggregation it flips a boolean expression to the opposite value.

Remember
$not = logical negation

Query (negate a field operator):
  { price: { $not: { $lt: 10 } } }
  // keep docs where price is NOT < 10

Aggregation (flip a boolean):
  { $not: [ { $gt: [ "$price", 10 ] } ] }
  // true when price is NOT > 10

5  not < 10 → false (dropped)
15 not < 10 → true  (kept)

sibling tips:
  simple ≠ value → prefer $ne
  many exclusions → consider $nor
NeedPrefer
Negate an operator$not
Not equal to a value$ne
None of several conditions$nor

How to Use $not in MongoDB

Pick query mode (invert a field operator) or expression mode (boolean flip).

  1. For filtering: wrap an operator under a field — { price: { $not: { $lt: 10 } } }.
  2. For a boolean: use $addFields / $project with { $not: [ expr ] }.
  3. Put one expression inside the aggregation array form.
  4. Prefer $ne for simple “not equal to this value” checks.
PieceRequired?Role
Query form—{ field: { $not: { op... } } }
Expression form—{ $not: [ expr ] } → boolean
Inner conditionYesWhat you want to invert

Pseudocode

Pseudocode
// Filter mode
for each document:
    if NOT (field matches operator-expression):
        keep document
    else:
        drop document

// Expression mode
for each document:
    result = NOT evaluate(expression)   // true ↔ false
    emit { ..., field: result }

Cheat Sheet

Keep these facts handy while writing NOT conditions.

TopicDetail
MeaningLogical NOT / invert a condition
Query syntax{ field: { $not: { $lt: 10 } } }
Agg syntax{ $not: [ <expression> ] }
vs $ne$ne = not equal to a value
Missing fieldQuery $not can match docs without the field
Agg falseyfalse, null, 0, undefined
Sample checkprice: { $not: { $lt: 10 } } keeps 15, drops 5

Live Preview

Watch query $not test “price is not less than 10”.

Simulates { price: { $not: { $lt: 10 } } }.

Result
price = 15
inner: price < 10 → false
$not → true (kept)

Worked Walkthrough — Not Less Than 10

Keep products whose price is not less than 10 (same idea as “price ≥ 10”, plus missing-field quirks).

Calculation
Documents:
  { name: "Pen",  price: 5 }
  { name: "Book", price: 15 }

Filter: { price: { $not: { $lt: 10 } } }

Pen:  5  < 10 → true  → NOT → false → drop
Book: 15 < 10 → false → NOT → true  → keep

Result: only Book

MongoDB Examples

We use two products. First filter with query $not, then flip a boolean in aggregation. Run these in mongosh after inserting the sample data once.

Example 1 — Sample Data

Imagine two products: a Pen at 5 and a Book at 15.

mongosh
db.products.insertMany([
  { _id: 1, name: "Pen", price: 5 },
  { _id: 2, name: "Book", price: 15 }
]);

How the Data Is Stored in MongoDB

After the insert, MongoDB keeps two documents in products. Run find() to see them:

mongosh
db.products.find()

Read those documents like this:

FieldPenBook
_id12
name"Pen""Book"
price515
Picture
NOT (price < 10)

  Pen 5   → 5 < 10 is true  → NOT → drop
  Book 15 → 15 < 10 is false → NOT → keep

How It Works

  1. Insert once. insertMany adds two product documents to products.
  2. Prices are numbers. Ready for comparison operators inside $not.
  3. find() shows both rows. The next example keeps only the negated match.

Example 2 — Filter with $not

Goal: return products whose price is not less than 10.

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

How It Works

  1. Inner test first. { $lt: 10 } asks “is price < 10?”.
  2. Then invert. $not keeps documents where that answer is false.
  3. Result. Book (15) stays; Pen (5) is dropped.

Example 3 — Boolean Expression

Same products, but keep every document and add a flag: “price is not greater than 10”.

mongosh
db.products.aggregate([
  {
    $addFields: {
      notOverTen: {
        $not: [
          { $gt: ["$price", 10] }
        ]
      }
    }
  }
]);

How It Works

  1. Inner compare. $gt checks price > 10.
  2. Wrap with $not. Aggregation form needs an array: { $not: [ ... ] }.
  3. Flip the boolean. Pen: not (5>10) → true. Book: not (15>10) → false.
  4. Nothing is dropped. Both rows stay; only the flag differs.

Edge Cases & Pitfalls

Check these before using $not in production queries.

missing

Missing fields can match

Query $not may keep documents that do not have the field. That differs from $lte, which usually requires the field.

syntax

Agg form needs an array

Write { $not: [ expr ] }, not a bare expression. Query form wraps an operator under a field name instead.

ne

Prefer $ne for simple inequality

{ status: { $ne: "archived" } } is clearer than { status: { $not: { $eq: "archived" } } }.

falsey

Agg treats 0 and null as false

In aggregation, $not of 0 or null is true. Non-zero numbers count as true.

Performance Notes

ApproachCostBest for
Positive range ($gte)Often clearer / index-friendlyWhen equivalent to negated $lt
Query $notDepends on inner op + missing fieldsNegating regex / complex operators
Agg boolean $notPer-document flipFlags without dropping rows

When $not: { $lt: x } is logically “≥ x” for existing numbers, consider writing $gte directly for clearer intent.

Key Takeaways

  • Role: logical NOT — invert a condition or boolean.
  • Query: { field: { $not: { op... } } }.
  • Expression: { $not: [ expr ] } → boolean.
  • Tip: use $ne for simple “not equal”.

One line: $not answers “is this condition false?”—as a filter or as a boolean.

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 it returns the opposite boolean of an expression.
Query form: { field: { $not: { <operator-expression> } } }. Aggregation form: { $not: [ <expression> ] }. The aggregation form wraps one expression in an array.
$ne compares a field to one value: { status: { $ne: "archived" } }. $not negates an operator expression: { price: { $not: { $lt: 10 } } }. Prefer $ne for simple inequality.
$not negates one operator expression on a field. $nor takes an array of separate conditions and matches when none of them match. Use $nor for multiple independent exclusions.
Yes. Documents that do not contain the field can match a $not condition. That differs from $lte / $gte, which usually require the field to exist.
false, null, 0, and undefined are treated as false (so $not returns true). Other values—including non-zero numbers and arrays—are treated as true.

Next: $objectToArray

Turn an object into an array of { k, v } documents.

$objectToArray tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

9 people found this page helpful