The $nin operator matches documents when a field’s value is not in a specified list. Use it in $match to exclude multiple statuses, categories, or IDs in one query instead of chaining many $ne conditions.
01
Exclusion List
Block multiple values.
02
Two Syntax Forms
Query filter vs expression.
03
$match Stage
Filter out unwanted rows.
04
Array Fields
Excludes on array elements.
05
Use Cases
Blocklists, cleanup.
06
$in Opposite
Inverse of membership.
Fundamentals
Definition and Usage
In MongoDB, the $nin operator selects documents where a field’s value does not appear in a specified array. For example, { status: { $nin: [ "archived", "cancelled" ] } } returns every document whose status is neither "archived" nor "cancelled". It is the direct opposite of $in.
In aggregation expressions, { $nin: [ "$role", [ "guest", "banned" ] ] } returns true when the field value is not in the array. This is useful for access checks and conditional labeling.
💡
Beginner Tip
Think of $nin as “not in this list.” It is cleaner than writing { $and: [ { status: { $ne: "a" } }, { status: { $ne: "b" } } ] }. When you need to include values instead, use $in.
Foundation
📝 Syntax
$nin has two forms depending on where you use it:
Query form (in $match or find filters)
mongosh
{ field: { $nin: [ <value1>, <value2>, ... ] } }
Aggregation expression form (in $project, $cond, etc.)
mongosh
{ $nin: [ <expression>, <array expression> ] }
Syntax Rules
Query form — keeps documents where the field does not equal any value in the array.
Expression form — returns true when the first operand is not found in the second array.
If the field is an array, a document is excluded when any element in the field array matches a listed value.
Values must match BSON types exactly (no automatic type conversion).
An empty $nin array { field: { $nin: [] } } matches all documents.
Query $nin also matches documents where the field is missing or null.
When the field is an array, $nin excludes the document if any tag matches a value in the exclusion list. This is the array-field counterpart to single-value exclusion.
Applications
🚀 Use Cases
Status blocklists — hide archived, cancelled, or deleted records from active views.
Category filtering — exclude discontinued or internal products from public catalogs.
Access control — flag users whose roles are not in an allowed set (or are in a denied set).
Content moderation — filter out documents tagged with blocked keywords.
🧠 How $nin Works
1
MongoDB reads the field and list
In $match, it checks a field against an exclusion array. In expressions, it evaluates a value and an array operand.
Input
2
$nin checks membership
MongoDB tests whether the field value appears in the array. If it does, the document is excluded (query) or false is returned (expression).
Compare
3
Non-matching documents pass
In $match, documents outside the blocklist continue. In expressions, true means “not in the list.”
Output
=
🚫
Clean exclusion filters
You get documents or boolean flags with unwanted values filtered out efficiently.
Wrap Up
Conclusion
The $nin operator is the standard way to exclude multiple values in MongoDB queries. It is the inverse of $in and more concise than chaining multiple $ne conditions.
Remember that an empty $nin array matches everything, query form includes missing fields, and expression form returns a boolean. Next in the series: $nor.
Use $nin when excluding two or more values from one field
Pair with $exists: true when missing fields should not match
Use expression $nin inside $cond for blocklist logic
Keep BSON types consistent in the exclusion array
Prefer $in for allowlists and $nin for blocklists
❌ Don’t
Use $nin with an empty array expecting no matches (it matches all)
Forget that query $nin includes documents without the field
Chain many $ne conditions when one $nin suffices
Confuse query syntax with expression syntax
Mix string and number types in the exclusion list
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $nin
Use these points when building exclusion filters in MongoDB.
5
Core concepts
🚫01
Not In List
Exclude many values.
Purpose
📝02
Two Forms
Query vs expression.
Syntax
🔄03
Inverse of $in
Block vs allow.
Related
⚠04
Empty Array
Matches everything.
Edge case
📑05
Array Fields
Any element counts.
Arrays
❓ Frequently Asked Questions
$nin matches documents where a field does not equal any value in a given array. In query filters it excludes listed values. In aggregation expressions it returns true when the first value is not found in the second array.
Query form: { field: { $nin: [ value1, value2, ... ] } }. Aggregation expression form: { $nin: [ <expression>, <array expression> ] }. The query form filters documents; the expression form returns true or false.
$ne excludes a single value: { status: { $ne: "archived" } }. $nin excludes any value in an array: { status: { $nin: ["archived", "cancelled"] } }. Use $nin when you need to exclude multiple values at once.
Yes. Like $ne, query $nin also matches documents that do not contain the field or where the field is null. Combine with $exists: true if you only want documents with the field present.
An empty array { field: { $nin: [] } } matches all documents, because no excluded values exist. This is the opposite of $in, where an empty array matches nothing.