0
0
MongodbHow-ToBeginner · 3 min read

How to Use $exists Operator in MongoDB for Field Existence Checks

In MongoDB, use the $exists operator in a query to check if a field is present or missing in documents. It takes a boolean value: true to find documents where the field exists, and false to find documents where it does not exist.
📐

Syntax

The $exists operator is used inside a query filter to test for the presence of a field in documents.

Its basic syntax is:

  • { field: { $exists: } }

Where:

  • field is the name of the field you want to check.
  • $exists is the operator.
  • <boolean> is true to find documents where the field exists, or false to find documents where it does not.
json
{ field: { $exists: true } }
💻

Example

This example shows how to find documents in a collection where the field email exists and where it does not exist.

mongodb
db.users.insertMany([
  { name: "Alice", email: "alice@example.com" },
  { name: "Bob" },
  { name: "Charlie", email: "charlie@example.com" },
  { name: "David" }
])

// Find documents where 'email' field exists
const withEmail = db.users.find({ email: { $exists: true } }).toArray()

// Find documents where 'email' field does NOT exist
const withoutEmail = db.users.find({ email: { $exists: false } }).toArray()

withEmail
withoutEmail
Output
[ { "_id": ObjectId("..."), "name": "Alice", "email": "alice@example.com" }, { "_id": ObjectId("..."), "name": "Charlie", "email": "charlie@example.com" } ] [ { "_id": ObjectId("..."), "name": "Bob" }, { "_id": ObjectId("..."), "name": "David" } ]
⚠️

Common Pitfalls

Common mistakes when using $exists include:

  • Using $exists without a boolean value, which is invalid.
  • Confusing $exists: false with fields that have null values; $exists checks presence, not value.
  • Expecting $exists to check nested fields without specifying the full path.

Example of wrong and right usage:

mongodb
// Wrong: missing boolean value
// db.collection.find({ email: { $exists } }) // Invalid syntax

// Right: with boolean value
// db.collection.find({ email: { $exists: true } })

// Wrong: expecting $exists:false to find null values
// db.collection.find({ email: { $exists: false } }) // Finds documents where 'email' is missing, not null

// To find null values use:
// db.collection.find({ email: null })
📊

Quick Reference

UsageDescriptionExample
Check if field existsFind documents where a field is present{ field: { $exists: true } }
Check if field missingFind documents where a field is absent{ field: { $exists: false } }
Check nested fieldUse dot notation for nested fields{ 'address.city': { $exists: true } }
Difference from nullField can exist but be null; $exists checks presence onlyUse { field: null } to find null values

Key Takeaways

Use $exists with true or false to check if a field is present or missing in documents.
$exists only checks if a field exists, not its value or if it is null.
Always provide a boolean value with $exists; omitting it causes errors.
Use dot notation with $exists to check nested fields inside documents.
To find fields with null values, query with { field: null }, not $exists.