MongoDB $nor Operator

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

What Is $nor?

$nor is a MongoDB query operator for logical NOR: keep a document only when none of the listed conditions match. Same idea as NOT (A OR B OR …).

Remember
$nor = none of these conditions match

{ $nor: [ { cond1 }, { cond2 }, ... ] }

Example:
  { $nor: [ { price: 1.99 }, { sale: true } ] }

keep if:
  price is not 1.99  AND  sale is not true
  (missing fields often count as "failed" conditions)

$or  → at least one true
$nor → zero true
UsePrefer
Fail every listed condition$nor
Pass at least one condition$or
Negate one expression$not

How to Use $nor in MongoDB

List the conditions you want to reject, then keep docs that fail all of them.

  1. Write each unwanted situation as its own query object.
  2. Wrap them in { $nor: [ ... ] }.
  3. Use that filter in find() or $match.
  4. Add $exists when missing fields should not quietly pass.
PieceRequired?Role
$norYesLogical NOR wrapper
Condition arrayYes (1+)Predicates that must all fail
$existsNoControl missing-field behavior

Pseudocode

Pseudocode
conditions = [cond1, cond2, ...]

for each document:
    for each condition in conditions:
        if condition matches document:
            drop document
            continue outer
    keep document   // none matched

Cheat Sheet

Keep these facts handy while writing NOR filters.

TopicDetail
TypeQuery / $match predicate
Syntax{ $nor: [ {…}, {…} ] }
MeaningNone of the conditions match
LogicNOT (A OR B OR …)
Opposite$or
Missing fieldsOften help a condition “fail”
Sample$nor: [{ price: 1.99 }, { sale: true }]

Live Preview

Filter with $nor: [{ price: 1.99 }, { sale: true }]. Pick a document shape and see KEEP or DROP.

Simulates { $nor: [ { price: 1.99 }, { sale: true } ] } (same idea as the mongosh examples below).

Result
doc = { price: 5, sale: false }
price==1.99? false
sale==true?  false
$nor → KEEP

Worked Walkthrough — Not Sale, Not $1.99

Reject items that are priced at 1.99 or marked sale: true. Keep only items that fail both checks.

Calculation
Conditions to reject:
  A: price == 1.99
  B: sale == true

$nor means: fail A AND fail B

{ price: 5,    sale: false } → fail A, fail B → KEEP
{ price: 1.99, sale: false } → pass A         → DROP
{ price: 5,    sale: true  } → pass B         → DROP
{ } (missing both)           → fail A, fail B → KEEP

MongoDB Examples

We use a small inventory collection. First a two-condition $nor, then a three-condition filter. Run these in mongosh after inserting the sample data once.

Example 1 — Sample Data

Imagine four inventory items with different price and sale flags. We save them in a collection named inventory.

mongosh
db.inventory.insertMany([
  { _id: 1, item: "abc", price: 1.99, sale: false, qty: 30 },
  { _id: 2, item: "jkl", price: 5.00, sale: true,  qty: 10 },
  { _id: 3, item: "xyz", price: 5.00, sale: false, qty: 25 },
  { _id: 4, item: "mno", price: 1.99, sale: true,  qty: 5 }
]);

How the Data Is Stored in MongoDB

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

mongosh
db.inventory.find()

Read one document like this:

Field in MongoDBValue (doc 3)Plain English
price5.00Unit price
salefalseOn sale?
qty25Stock quantity

How It Works

  1. Insert once. insertMany adds four inventory documents.
  2. Two key flags. price and sale drive the NOR examples.
  3. find() shows the same shape. Ready for $nor.

Example 2 — Two Conditions

Goal: keep items that are not priced 1.99 and not on sale.

mongosh
db.inventory.find({
  $nor: [
    { price: 1.99 },
    { sale: true }
  ]
});

How It Works

  1. List rejections. Condition A is price: 1.99; condition B is sale: true.
  2. $nor keeps failures only. A document must fail both A and B.
  3. Only xyz survives. Price is 5 (not 1.99) and sale is false.
  4. Others drop. abc/mno hit price 1.99; jkl/mno hit sale true.

Example 3 — Three Conditions

Same idea with a third rejection: also drop low stock (qty < 20).

mongosh
db.inventory.aggregate([
  {
    $match: {
      $nor: [
        { price: 1.99 },
        { sale: true },
        { qty: { $lt: 20 } }
      ]
    }
  }
]);

How It Works

  1. Add a third rejection. { qty: { $lt: 20 } } must also fail (qty ≥ 20).
  2. Still only xyz. It fails all three: not 1.99, not on sale, qty 25.
  3. Works in $match. Same operator form as find().
  4. Cross-field logic. This is where $nor beats a single-field $nin.

Edge Cases & Pitfalls

Check these before using $nor in production queries.

missing

Missing fields often “fail” conditions

A missing price fails { price: 1.99 }, which can help the doc satisfy $nor. Use $exists when presence matters.

vs

Don’t confuse with $or / $and

$or needs one success. $and needs every success. $nor needs every failure.

field

One field → consider $nin / $ne

Excluding several values on the same field is clearer with $nin. Reserve $nor for multi-field logic.

expr

Not an aggregation expression

Use $nor in query / $match. Inside $expr, combine $not with $or instead.

Performance Notes

ApproachCostBest for
$nor aloneCan be broadMulti-field “none of these”
$nor + selective filtersUsually betterNarrow first, then NOR
Positive filtersOften clearer/fasterWhen you can say what you want

Negative logic is often less selective than positive filters. Prefer stating what you want with $and / range checks when possible, and keep $match early in aggregations.

Key Takeaways

  • Role: keep docs that fail every listed condition.
  • Logic: NOT (A OR B OR …).
  • Best for multi-field exclusions.
  • Watch missing fields unless you use $exists.

One line: $nor answers “does this document avoid every listed bad case?”

Frequently Asked Questions

$nor performs a logical NOR on an array of query conditions. A document matches only when none of those conditions match. It is equivalent to NOT (condition1 OR condition2 OR …).
The syntax is { $nor: [ { <condition1> }, { <condition2> }, ... ] }. Each array element is a full query expression, such as { price: 1.99 } or { qty: { $lt: 20 } }.
$nor is a query operator. Use it in find() and in the $match stage of aggregation. It is not a standalone aggregation expression—use $not with $or inside $expr for similar expression logic.
$or matches when at least one condition is true. $nor matches when none of the conditions are true. They are logical opposites for the same condition list.
Use $nin to exclude several values on one field. Use $nor when conditions span different fields or different operators, such as price equals 1.99 OR sale is true—and you want neither.
Yes. For many comparisons, a missing field fails that condition, which can help the document satisfy $nor. Use $exists when you need fields to be present.

Next: $not

Invert one query expression to keep the opposite matches.

$not tutorial →

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