$not (Logical NOT)
Last updated: 2026-10-09
$not (Logical NOT)
$not negates an operator expression, returning documents that fail the inner condition. `{ price: { $not: { $gt: 50 } } }` matches products priced at most 50. $not does not match documents missing the field, and it requires an operator expression inside — not a regex literal.
| Category | Logical |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ <field>: { $not: { <operator expression> } } }
⚙️ Parameters
| field | The document field to match. |
|---|---|
| operator expression | The operator condition to negate, e.g. { $gt: 50 }. |
Returns:Cursor of matching documents
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $not: price not greater than 50
db.products.find({ price: { $not: { $gt: 50 } } })
// Equivalent: $lte is more direct
db.products.find({ price: { $lte: 50 } })
Output:
// Returns products priced at most 50; missing-field documents are excluded
📌 Notes
- $not does not match missing fields; combine with $exists to include them
- Prefer the direct operator ($lte, $ne) when it expresses the negation more clearly
- $not accepts operator expressions only ($gt, $regex), not literals
🏷️ Related Tags
💡 Related Cases
Document Query Basics: The `find` Method and Projections
Query Operators: A Complete Guide to the 30+ Operator
📚 Related Tutorials
❓ FAQ
QWhat is the difference between $not and $ne?
A$ne means not-equal-to-a-value; $not negates a whole operator expression, e.g. { $not: { $gt: 50 } }.
QDoes $not match documents where the field is missing?
ANo. A missing field fails the inner condition, so the negation still does not match.
QCan $not negate a regex?
AYes, via { $not: { $regex: '...' } }, but not as { $not: /pattern/ }.