The $exists operator checks whether a field is present in a document. Use it in $match or find() to filter records that have (or lack) a specific field — essential for schema checks and data quality workflows.
01
Field Presence
Check if a field exists.
02
Syntax
{ field: { $exists: bool } }
03
$match Stage
Filter in aggregation pipelines.
04
true vs false
Present vs missing fields.
05
Use Cases
Data quality, optional fields.
06
Null vs Missing
Exists ≠ non-null value.
Fundamentals
Definition and Usage
In MongoDB, the $exists operator tests whether a document contains a given field. Use { email: { $exists: true } } to find documents where email is present, or { phone: { $exists: false } } to find documents where phone is completely absent. This is especially useful when documents in the same collection have different shapes (optional or legacy fields).
💡
Beginner Tip
$exists: true matches documents where the field is present even if its value isnull. To find documents with a real value, combine $exists: true with { field: { $ne: null } }.
Foundation
📝 Syntax
The $exists operator takes a boolean value that controls whether the field must be present or absent:
mongosh
{ field: { $exists: <boolean> } }
Syntax Rules
$exists: true — matches documents where the field is present (value can be anything, including null).
$exists: false — matches documents where the field is missing entirely.
Use in find() queries and the $match stage of aggregation pipelines.
Works on top-level fields and nested fields using dot notation (e.g., "address.city").
Combine with $and, $or, and other query operators for complex filters.
⚠️ Missing Field vs null Value
These are different in MongoDB. Know which one you need:
{ email: null } → field exists with null value ($exists: true)
{ name: "Alice" } (no email key) → field is missing($exists: false)
Walk through sample user documents with optional fields, filter by field presence with $match, and learn the difference between missing fields and null values.
📚 Check Field Presence
Use a users collection where some documents have optional fields and others do not.
Sample Input Documents
Suppose you have a users collection with mixed field shapes:
Dot notation works with $exists for nested documents. This matches any document where address.city is present, regardless of its value.
Applications
🚀 Use Cases
Data quality audits — find documents missing required fields like email, phone, or address.
Schema exploration — discover which optional fields exist across documents in a collection.
Migration validation — verify that new fields were added after a schema update.
Conditional processing — filter records before downstream stages that depend on specific fields being present.
🧠 How $exists Works
1
MongoDB inspects the document
For each document, MongoDB checks whether the specified field key is stored — at the top level or via dot notation for nested paths.
Input
2
$exists evaluates true or false
With $exists: true, documents where the field is present pass. With $exists: false, only documents without the field pass.
Check
3
Matching documents continue in the pipeline
In $match, only matching documents move to the next stage. Non-matching documents are filtered out.
Filter
=
🔍
Targeted document set
You get only documents that have (or lack) the field you care about — ready for cleanup, enrichment, or reporting.
Wrap Up
Conclusion
The $exists operator is an essential query tool for working with MongoDB’s flexible schema. It lets you find documents that contain (or lack) specific fields — critical for data quality checks, optional field handling, and migration validation.
For beginners, remember: $exists: true means the field is present (even if null), $exists: false means the field is missing entirely, and combining with $ne: null gives you documents with real values.
Use $exists: true to find documents with a field present
Use $exists: false to find documents where a field is missing
Combine with $ne: null when you need a non-null value
Use dot notation for nested fields like "address.city"
Place $exists early in pipelines with $match for performance
❌ Don’t
Assume $exists: true excludes null values (it does not)
Use $exists inside $project (it is a query operator, not an expression)
Confuse missing fields with null values
Forget that $exists: false won’t match field: null
Skip indexes on fields you frequently query with $exists
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $exists
Use these points when checking field presence in MongoDB.
5
Core concepts
🔢01
Field Presence
Checks if a key exists.
Purpose
📝02
Boolean Flag
{ field: { $exists: bool } }
Syntax
🛠03
$match Filter
Query and aggregation.
Usage
🔍04
Data Quality
Find missing fields.
Use case
⚠05
Null ≠ Missing
Exists true matches null.
Edge case
❓ Frequently Asked Questions
$exists checks whether a field is present in a document. Use { field: { $exists: true } } to match documents that contain the field, or { field: { $exists: false } } to match documents where the field is missing entirely.
The syntax is { field: { $exists: <boolean> } }. Pass true to find documents where the field exists, or false to find documents where the field is absent.
Yes. A field with a null value still exists in the document. { email: { $exists: true } } matches documents where email is null. To find non-null values, combine with { email: { $ne: null } }.
$exists is a query operator used in find() filters and in the $match stage of aggregation pipelines. It is not an aggregation expression operator for $project.
A missing field is not stored in the document at all. A null field is stored with an explicit null value. $exists: false matches missing fields only; $exists: true matches both null and non-null values.