$exists (Field Existence)
Last updated: 2026-10-09
$exists (Field Existence)
$exists filters documents by whether a field is present. `{ $exists: true }` matches documents where the field exists and is not null; `{ $exists: false }` matches documents where it is missing. It is the primary tool for missing-field problems and pairs with $ne/$gt, which otherwise skip missing fields.
| Category | Element & Evaluation |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ <field>: { $exists: <boolean> } }
⚙️ Parameters
| field | The document field to test for existence. |
|---|---|
| boolean | true matches fields that exist, false matches missing fields. |
Returns:Cursor of matching documents
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $exists: users who have an email
// Field must exist and be non-null
db.users.find({ email: { $exists: true } })
// Users without an email field
db.users.find({ email: { $exists: false } })
Output:
// The first returns users with an email field; the second returns users without it
📌 Notes
- $exists: true does not match a field whose value is null
- Combine with $ne/$gt/$nin to compensate for their missing-field behavior
- Use dotted paths for nested fields: { 'address.city': { $exists: true } }
🏷️ Related Tags
💡 Related Cases
📚 Related Tutorials
❓ FAQ
QDoes $exists: true match null values?
ANo. A field present with a null value fails $exists: true; use $type: 10 or $eq: null instead.
QHow do $exists and $ne work together?
A{ $ne: value, $exists: true } means field exists and differs — preventing $ne from skipping missing fields.
QHow do I check whether an array field exists?
ASame pattern: { $exists: true }; an empty array still counts as existing.