MongoDB $exists and $type
$exists and $type β schema questions on schemaless data. Β· 8 practice questions Β· Cheat sheet β
Collections without a fixed schema need a way to ask about shape, not just value. $exists checks whether a field is present at all, and $type checks what kind of value it holds: "string", "int", "double", "array", "date", "null" and so on. Together they are how you check if a field exists in MongoDB and whether old documents still carry a value in the wrong type.
The subtle part is the three ways a value can be "empty": a missing field, a field set to null, and a field set to false or 0. { newsletter: null } matches both missing and null; { newsletter: { $exists: false } } matches only missing; { newsletter: false } matches only an explicit false. Picking the right one is the whole topic.
MongoDB $exists and $type practice questions
- Find customers that have a
newsletterfield at all. easy customers - Find customers that have no
newsletterfield. easy customers - Find orders that used a coupon (the
couponfield is present). easy orders - Find orders where
couponis a string. Return_idandcoupononly. medium orders - Find customers whose
newsletteris a boolean value. medium customers - Find movies where
genresis stored as an array. medium movies - Find customers who are NOT subscribed to the newsletter β counting a missing
newsletterfield as not subscribed. hard customers - Confirm every restaurant has a street: find restaurants where
address.streetdoes NOT exist. hard restaurants
Each question is answered in a live mongo-shell editor on this page; the result is checked against a reference solution.
$exists and $type cheat sheet
Does the field exist at all?
db.orders.find({ coupon: { $exists: true } })
db.orders.find({ coupon: { $exists: false } })
$exists: true matches even when the value is null. It is asking about the
key, not the value.
What type is it?
db.products.find({ price: { $type: "number" } })
db.movies.find({ genres: { $type: "array" } })
db.customers.find({ newsletter: { $type: "bool" } })
Common aliases: "double", "string", "object", "array", "bool",
"date", "null", "int", "long", "decimal".
Missing vs null vs false
| Query | missing key | null | false |
|---|---|---|---|
{ f: { $exists: false } } | β | β | β |
{ f: null } | β | β | β |
{ f: { $ne: true } } | β | β | β |
That first column is why $ne surprises people.
Common questions
How do I check if a field exists in MongoDB?
{ coupon: { $exists: true } } matches documents that have the field, even if its value is null. { coupon: { $exists: false } } matches documents without it. Combine with $ne: null when you need the field to hold a real value.What's the difference between a missing field and null?
{ f: null } matches both. { f: { $exists: false } } matches only missing. { f: { $type: "null" } } matches only an explicit null.How do I find documents where a field has the wrong type?
$type with a type name or an array of names: { seats: { $type: "string" } } finds numbers stored as text, and { seats: { $type: ["int", "double"] } } finds any numeric value. It is a quick way to audit data imported from CSV.Continue:β Logical OperatorsPattern Matching β
MongoDB topics:Finding & ProjectionComparison OperatorsLogical OperatorsPattern MatchingSort, Limit & SkipArray QueriesEmbedded DocumentsAggregation BasicsGrouping & Accumulators$unwind & Array PipelinesJoins & Advanced StagesFull cheat sheet