$exists — MongoDB Technical Interview Questions & Answers
Matching documents where a field is present (or absent).
$exists
- Simple meaning: $exists checks whether a field is present (even if null, depending on value).
- How to use: db.students.find({ city: { $exists: true } }) students who have a city field. false = field missing.
- Try this: insert one student without city, one with city, then $exists true/false.
- Common mistake: using $exists to mean “not empty string”. Empty string still exists.
- Interview tip: Schema is flexible — $exists is how you find docs that gained a new field.
Technical Interview Tip: Flexible schema → $exists to find who has the field.
Code Examples for $exists
$exists — working example
db.users.find({ phone: { $exists: true } })
db.users.find({ phone: { $exists: false } })$exists checks whether a field is present (even if null — combine with $ne: null if needed).
How to Explain $exists to an Interviewer
What does $exists do?
Match documents where a field is present or absent