MongoDB $nor Operator

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

What You’ll Learn

The $nor operator performs a logical NOR on an array of query conditions. A document matches only when none of the conditions are true — the opposite of $or.

01

Logical NOR

None may match.

02

Array Syntax

Full conditions in array.

03

Query Operator

find() and $match.

04

Multi-Field

Cross-field exclusions.

05

Use Cases

Blocklists, eligibility.

06

vs $or

Opposite of OR logic.

Definition and Usage

In MongoDB, the $nor operator selects documents where no sub-expression in its array matches. Think of it as: “exclude documents that satisfy condition A or condition B.” Formally, $nor is equivalent to NOT (A OR B OR ...).

This is especially useful when exclusion rules span different fields. For example, keep products that are neither out of stock nor priced below $10.

💡
Beginner Tip

$nor is a query operator for find() and $match. It is not an aggregation expression like $and or $or in $project. For NOR logic inside $expr, use { $not: { $or: [ ... ] } } instead.

📝 Syntax

The $nor operator takes an array of query expressions:

mongosh
{ $nor: [ <condition1>, <condition2>, ... ] }

Example (in find or $match)

mongosh
{
  $nor: [
    { inStock: false },
    { price: { $lt: 10 } }
  ]
}

Syntax Rules

  • Array required — wrap each condition as a separate object in the array.
  • Query operator — use in find() and $match (not in $project directly).
  • Each array element is a complete query expression (field + operator).
  • A document matches when zero sub-expressions match.
  • If any one condition matches, the document is excluded.
  • For excluding multiple values on one field, $nin is often simpler.

💡 $nor vs $or vs $not vs $nin

$or — at least one condition must match (include if A OR B)
$nor — no condition may match (exclude if A OR B)
$not — negates a single condition: { price: { $not: { $lt: 10 } } }
$nin — field not in value list (single-field exclusion)

⚡ Quick Reference

QuestionAnswer
Operator typeLogical query operator (NOR)
Syntax{ $nor: [ cond1, cond2, ... ] }
Match ruleNone of the conditions may match
Common usagefind(), $match
Logical equivalentNOT (A OR B OR ...)
Multi-field
{
  $nor: [
    { inStock: false },
    { price: { $lt: 10 } }
  ]
}

Cross-field exclusion

User filter
{
  $nor: [
    { status: "inactive" },
    { age: { $lt: 18 } }
  ]
}

Not inactive AND not minor

In $match
$match: {
  $nor: [ ... ]
}

Pipeline filter

$expr alternative
$expr: {
  $not: {
    $or: [ ... ]
  }
}

NOR in expressions

Examples Gallery

Exclude products that are out of stock or too cheap, filter eligible users, and apply $nor in aggregation pipelines.

📚 Product Catalog Filter

Keep products that are in stock and priced at $10 or above using $nor.

Sample Input Documents

Suppose you have a products collection:

mongosh
[
  { "_id": ObjectId("609c26812e9274a86871bc6a"), "name": "Widget",  "inStock": true,  "price": 25 },
  { "_id": ObjectId("609c26812e9274a86871bc6b"), "name": "Cheap",   "inStock": true,  "price": 5 },
  { "_id": ObjectId("609c26812e9274a86871bc6c"), "name": "Old",     "inStock": false, "price": 50 },
  { "_id": ObjectId("609c26812e9274a86871bc6d"), "name": "Clearance","inStock": false, "price": 3 }
]

Example 1 — $nor in find()

Exclude products that are out of stock or priced below $10:

mongosh
db.products.find({
  $nor: [
    { inStock: false },
    { price: { $lt: 10 } }
  ]
})

How It Works

  • Widget — in stock, price 25 → neither condition matches → included.
  • Cheap — price < 10 matches → excluded.
  • Old — out of stock matches → excluded.
  • Clearance — both conditions match → excluded.

📈 Practical Patterns

User eligibility, aggregation $match, and when to prefer other operators.

Example 2 — Eligible Users (Not Inactive, Not a Minor)

Find users who are neither inactive nor under 18:

mongosh
db.users.find({
  $nor: [
    { status: "inactive" },
    { age: { $lt: 18 } }
  ]
})

How It Works

Each condition is evaluated independently. If any condition matches, the user is excluded. Only users passing all negated rules remain.

Example 3 — $nor in an Aggregation $match Stage

Filter a pipeline early to drop unwanted orders:

mongosh
db.orders.aggregate([
  {
    $match: {
      $nor: [
        { status: "cancelled" },
        { status: "refunded" },
        { total: { $lte: 0 } }
      ]
    }
  },
  {
    $group: {
      _id: "$customerId",
      orderCount: { $sum: 1 },
      revenue: { $sum: "$total" }
    }
  }
])

How It Works

$nor in $match removes cancelled, refunded, and zero-total orders before grouping. This keeps downstream stages clean and efficient.

Example 4 — When to Use $nin Instead

When all exclusions are values on the same field, $nin is clearer:

mongosh
// These are equivalent for one field:
db.orders.find({
  status: { $nin: [ "cancelled", "refunded" ] }
})

db.orders.find({
  $nor: [
    { status: "cancelled" },
    { status: "refunded" }
  ]
})

How It Works

Prefer $nin for same-field value lists. Reserve $nor when conditions involve different fields or different operators.

Bonus — NOR Logic Inside $expr

When you need NOR behavior with field comparisons, use $not + $or:

mongosh
db.products.find({
  $expr: {
    $not: {
      $or: [
        { $eq: [ "$inStock", false ] },
        { $lt: [ "$price", 10 ] }
      ]
    }
  }
})

// Equivalent to the $nor example above

How It Works

$nor is not an aggregation expression operator. Inside $expr, wrap an $or with $not to achieve the same logical result with field-level comparisons.

🚀 Use Cases

  • Catalog filtering — exclude products that are out of stock or below a minimum price.
  • User eligibility — block users who are inactive or below a minimum age.
  • Order cleanup — remove cancelled, refunded, or invalid orders before reporting.
  • Complex exclusions — combine unrelated field conditions in one filter.

🧠 How $nor Works

1

MongoDB evaluates each condition

Every expression in the $nor array is tested against each document.

Input
2

$nor checks for zero matches

If any condition matches, the document fails. All conditions must fail for the document to pass.

Logic
3

Matching documents continue

In find() or $match, only documents where none of the NOR conditions matched are returned.

Output
=

Clean exclusion filters

Documents matching any blocked condition are removed from results.

Conclusion

The $nor operator is MongoDB’s logical NOR for query filters. Use it when you need to exclude documents that match any of several independent conditions, especially across different fields.

Remember it is query-only, opposite to $or, and often replaceable by $nin for same-field value lists. Next in the series: $not.

💡 Best Practices

✅ Do

  • Use $nor for cross-field exclusion rules
  • Put each condition in its own object inside the array
  • Prefer $nin when excluding values on one field
  • Use $not + $or inside $expr for expression-level NOR
  • Place $match with $nor early in pipelines

❌ Don’t

  • Use $nor directly inside $project (it is query-only)
  • Confuse $nor with $not (single-condition negation)
  • Forget that one matching condition excludes the document
  • Overuse $nor when simple $nin or $ne suffices
  • Mix up $nor (none match) with $and (all must match)

Key Takeaways

Knowledge Unlocked

Five things to remember about $nor

Use these points when building exclusion filters in MongoDB.

5
Core concepts
📝 02

Array Syntax

{ $nor: [...] }

Syntax
🔍 03

Query Only

find() and $match.

Usage
🔄 04

Opposite of $or

NOT (A OR B).

Logic
📑 05

Use $nin

Same-field lists.

Alternative

❓ Frequently Asked Questions

$nor performs a logical NOR on an array of query conditions. A document matches only when none of the conditions in the array match. It is equivalent to NOT (condition1 OR condition2 OR ...).
The syntax is { $nor: [ <condition1>, <condition2>, ... ] }. Each element is a full query expression, such as { status: "inactive" } or { price: { $lt: 10 } }.
$nor is a query operator. Use it in find() filters and in the $match stage of aggregation pipelines. It is not available as a standalone aggregation expression operator — use $not with $or inside $expr for similar logic.
$or matches documents where at least one condition is true. $nor matches documents where none of the conditions are true. They are logical opposites when applied to the same condition set.
Use $nin when excluding multiple values from one field: { status: { $nin: ["a", "b"] } }. Use $nor when conditions span different fields or use different operators: { $nor: [ { inStock: false }, { price: { $lt: 10 } } ] }.

Continue the Operator Series

Move on to $not for single-condition negation, or review $and for logical AND.

Next: $not →

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