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:
fieldis the name of the field you want to check.$existsis the operator.<boolean>istrueto find documents where the field exists, orfalseto 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
withoutEmailOutput
[
{ "_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
$existswithout a boolean value, which is invalid. - Confusing
$exists: falsewith fields that havenullvalues;$existschecks presence, not value. - Expecting
$existsto 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
| Usage | Description | Example |
|---|---|---|
| Check if field exists | Find documents where a field is present | { field: { $exists: true } } |
| Check if field missing | Find documents where a field is absent | { field: { $exists: false } } |
| Check nested field | Use dot notation for nested fields | { 'address.city': { $exists: true } } |
| Difference from null | Field can exist but be null; $exists checks presence only | Use { 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.