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

CategoryElement & Evaluation
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

{ <field>: { $exists: <boolean> } }

⚙️ Parameters

fieldThe document field to test for existence.
booleantrue 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

📚 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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏