πŸƒDBQuestpractice MongoDB queries
0 / 110

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

  1. Find customers that have a newsletter field at all. easy customers
  2. Find customers that have no newsletter field. easy customers
  3. Find orders that used a coupon (the coupon field is present). easy orders
  4. Find orders where coupon is a string. Return _id and coupon only. medium orders
  5. Find customers whose newsletter is a boolean value. medium customers
  6. Find movies where genres is stored as an array. medium movies
  7. Find customers who are NOT subscribed to the newsletter – counting a missing newsletter field as not subscribed. hard customers
  8. Confirm every restaurant has a street: find restaurants where address.street does 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

Querymissing keynullfalse
{ 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?

A missing field is absent from the document; null is a present field with no value. { 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?

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