MongoDB $exists Operator

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 4 Examples
Query

What You’ll Learn

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.

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 is null. To find documents with a real value, combine $exists: true with { field: { $ne: null } }.

📝 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)
Non-null check: { email: { $exists: true, $ne: null } }

⚡ Quick Reference

QuestionAnswer
Operator typeQuery operator
Syntax{ field: { $exists: <boolean> } }
trueField is present (may be null)
falseField is missing entirely
Common stages$match, find()
Field present
{
  email: { $exists: true }
}

Has email field

Field missing
{
  phone: { $exists: false }
}

No phone field

Non-null value
{
  email: {
    $exists: true,
    $ne: null
  }
}

Has real email

Nested field
{
  "address.city": {
    $exists: true
  }
}

Dot notation

Examples Gallery

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:

mongosh
[
  { "_id": ObjectId("609c26812e9274a86871bc6a"), "name": "Alice", "email": "alice@example.com" },
  { "_id": ObjectId("609c26812e9274a86871bc6b"), "name": "Bob", "email": null },
  { "_id": ObjectId("609c26812e9274a86871bc6c"), "name": "Charlie" }
]

Example 1 — Find Documents Where a Field Exists

Return users who have an email field (including those where email is null):

mongosh
db.users.aggregate([
  {
    $match: {
      email: { $exists: true }
    }
  }
])

How It Works

  • Alice and Bob both have an email key, so they match.
  • Charlie has no email field at all, so he is excluded.
  • Bob’s null value still counts as the field existing.

📈 Practical Patterns

Find missing optional fields, require non-null values, and audit data quality in pipelines.

Example 2 — Find Documents Where a Field Is Missing

Locate users who have no phone field at all:

mongosh
db.users.aggregate([
  {
    $match: {
      phone: { $exists: false }
    }
  }
])

How It Works

$exists: false only matches documents where the field is completely absent. A document with phone: null would not match this filter.

Example 3 — Require a Non-null Value

Find users with a real email address (field exists and is not null):

mongosh
db.users.aggregate([
  {
    $match: {
      email: { $exists: true, $ne: null }
    }
  }
])

How It Works

Combining $exists: true with $ne: null is the standard pattern when you need an actual value, not just the presence of the key.

Example 4 — Check a Nested Field with Dot Notation

Find profiles that include a city in their address:

mongosh
db.users.aggregate([
  {
    $match: {
      "address.city": { $exists: true }
    }
  }
])

How It Works

Dot notation works with $exists for nested documents. This matches any document where address.city is present, regardless of its value.

🚀 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.

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.

💡 Best Practices

✅ Do

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about $exists

Use these points when checking field presence in MongoDB.

5
Core concepts
📝 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.

Continue the Operator Series

Move on to $exp for exponential math, or review $eq for equality comparisons.

Next: $exp →

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