MongoDB $exists Operator

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

What Is $exists?

$exists is a MongoDB query predicate that matches documents where a field is present or missing. A field with value null still counts as present.

Remember
$exists = is this field on the document?

{ field: { $exists: true } }   // field is present (even if null)
{ field: { $exists: false } }  // field is missing

present + value  → matches $exists: true
present + null   → matches $exists: true
missing field    → matches $exists: false

Need a real value? → { field: { $ne: null } }
UsePrefer
Field present / missing$exists
Field has a non-null value$ne: null
Check missing inside an expression$type: "missing"

How to Use $exists in MongoDB

Put $exists on the field you care about inside find() or $match.

  1. Choose find() or an aggregation $match stage.
  2. Write { field: { $exists: true } } or false.
  3. Remember: true includes null; false means the key is absent.
  4. If you need a real value, add { field: { $ne: null } }.
PieceRequired?Role
Field pathYesWhich key to check
$existsYestrue = present, false = missing
$ne: nullNoExclude null (and missing) for real values

Pseudocode

Pseudocode
for each document:
    if $exists is true:
        keep document if field key is present
            // value may be string, number, null, …
    if $exists is false:
        keep document if field key is absent
            // not the same as value === null

Cheat Sheet

Keep these facts handy while checking field presence.

TopicDetail
TypeQuery predicate (find / $match)
Syntax{ field: { $exists: <boolean> } }
trueField present, including null
falseField missing only
Not for $projectUse $type: "missing" in expressions
Real value pattern{ field: { $ne: null } }
Not SQL EXISTSChecks a field, not a subquery

Live Preview

Pick a document shape and see which $exists queries match. Notice that null still counts as present.

Simulates $exists: true, $exists: false, and $ne: null on an email field.

Result
document = { name: "Ada", email: "[email protected]" }

$exists: true  → match
$exists: false → no match
$ne: null      → match

Worked Walkthrough — Missing vs Null

Three people. One has an email, one has email: null, and one has no email field at all.

Calculation
Ada:   { name: "Ada",   email: "[email protected]" }  // present + value
Grace: { name: "Grace", email: null }               // present + null
Alan:  { name: "Alan" }                             // missing

email: { $exists: true }
  → Ada, Grace

email: { $exists: false }
  → Alan

email: { $ne: null }
  → Ada only   (skips null and missing)

MongoDB Examples

We store those three people, find who has an email field, then find who is missing it. Run these in mongosh after inserting the sample data once.

Example 1 — Sample Data

Imagine a tiny people collection that mixes a real email, an explicit null, and a missing field.

mongosh
db.people.insertMany([
  { _id: 1, name: "Ada",   email: "[email protected]" },
  { _id: 2, name: "Grace", email: null },
  { _id: 3, name: "Alan" }
]);

How the Data Is Stored in MongoDB

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

mongosh
db.people.find()

Read those documents like this:

Documentemail fieldPlain English
Ada"[email protected]"Present with a real value
GracenullPresent, but empty on purpose
Alan(no key)Missing entirely
Picture
Field presence checklist:

  Ada   → email key yes, value yes
  Grace → email key yes, value null
  Alan  → email key no

How It Works

  1. Insert once. insertMany adds three people with different email shapes.
  2. Null is stored. Grace’s document still has an email key.
  3. Missing is absent. Alan has no email key at all—ready for $exists.

Example 2 — $exists: true

Goal: find everyone who has an email field, even if the value is null.

mongosh
db.people.find(
  { email: { $exists: true } },
  { _id: 0, name: 1, email: 1 }
);

How It Works

  1. Check the key. $exists: true asks “is email on this document?”
  2. Null still counts. Grace matches because the field is present.
  3. Missing is skipped. Alan has no email key, so he is out.
  4. Projection is optional. The second argument keeps only name and email.

Example 3 — $exists: false

Goal: find people with no email field at all. Same idea works in aggregation $match.

mongosh
db.people.aggregate([
  {
    $match: {
      email: { $exists: false }
    }
  },
  {
    $project: {
      name: 1,
      _id: 0
    }
  }
]);

How It Works

  1. Ask for absence. $exists: false keeps documents with no email key.
  2. Null is not missing. Grace still has email: null, so she does not match.
  3. Only Alan remains. His document never stored email.
  4. Same in find(). db.people.find({ email: { $exists: false } }) is equivalent for filtering.

Edge Cases & Pitfalls

Check these before using $exists in production queries.

null

Null is not missing

$exists: true matches null. Use $ne: null when you need a real value.

agg

Not an expression operator

You cannot put $exists inside $project math. In expressions, use $type: "missing".

sql

Not SQL EXISTS

MongoDB $exists checks a field on the current document. It does not run a subquery existence test.

index

$exists: false is hard to index

$exists: true can use a sparse index efficiently. $exists: false often needs a collection scan—filter early when you can.

Performance Notes

ApproachCostBest for
$exists: true + sparse indexOften very efficientFields present on a minority of docs
$exists: falseOften collection scanFinding incomplete documents
$ne: nullIndex-friendly when indexedNon-null real values

Prefer early $match filters. If you mostly query “field has a value,” $ne: null is often clearer and more index-friendly than $exists: true alone.

Key Takeaways

  • Role: field present (true) or missing (false).
  • Null: still exists—use $ne: null for real values.
  • Place: find / $match only (not $project math).
  • Expressions: use $type: "missing" instead.

One line: $exists answers “is this field on the document?”—not “does it have a useful value?”

Frequently Asked Questions

$exists matches documents that contain or do not contain a specified field. Use { field: { $exists: true } } for present fields, or { field: { $exists: false } } for documents where the field is missing entirely.
Yes. A field stored as null still exists. { email: { $exists: true } } matches both "[email protected]" and null. To require a non-null value, combine with { email: { $ne: null } }.
Missing means the field is not stored at all. Null means the field is stored with an explicit null value. $exists: false matches missing only. $exists: true matches null and non-null values.
$exists is a query predicate operator for find() filters and the $match stage. It is not an aggregation expression for $project. In expressions, check $type against "missing" instead.
No. SQL EXISTS checks whether a subquery returns rows. MongoDB $exists checks whether a field is present on a document.
Prefer { field: { $ne: null } }. That skips both missing fields and explicit nulls. $exists: true alone still includes null.

Next: $exp

Compute e raised to a power in an aggregation pipeline.

$exp 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