0
0
MongodbHow-ToBeginner · 2 min read

MongoDB Query to Find Documents Where Field Exists

Use the MongoDB query { fieldName: { $exists: true } } to find documents where the field fieldName exists.
📋

Examples

Input{ age: { $exists: true } }
Output[{ "name": "Alice", "age": 25 }, { "name": "Bob", "age": 30 }]
Input{ phone: { $exists: true } }
Output[{ "name": "Charlie", "phone": "123-456" }]
Input{ address: { $exists: true } }
Output[]
🧠

How to Think About It

To find documents where a field exists, think about checking if that field is present in each document. MongoDB provides the $exists operator to test if a field is present or not. You just specify the field name and set $exists to true to get all documents that have that field.
📐

Algorithm

1
Identify the field name you want to check for existence.
2
Create a query using the <code>$exists</code> operator with value true for that field.
3
Run the query on the collection to get all documents where the field exists.
4
Return the matching documents.
💻

Code

mongodb
db.collection.find({ age: { $exists: true } })
Output
[ { "_id": ObjectId("..."), "name": "Alice", "age": 25 }, { "_id": ObjectId("..."), "name": "Bob", "age": 30 } ]
🔍

Dry Run

Let's trace the query { age: { $exists: true } } through the collection.

1

Check first document

Document: { name: 'Alice', age: 25 } - Field 'age' exists, include this document.

2

Check second document

Document: { name: 'Bob', age: 30 } - Field 'age' exists, include this document.

3

Check third document

Document: { name: 'Charlie' } - Field 'age' does not exist, exclude this document.

DocumentField 'age' Exists?Include in Result?
{ name: 'Alice', age: 25 }YesYes
{ name: 'Bob', age: 30 }YesYes
{ name: 'Charlie' }NoNo
💡

Why This Works

Step 1: Using $exists operator

The $exists operator checks if a field is present in a document, regardless of its value.

Step 2: Setting $exists to true

Setting $exists: true means the query matches documents where the field is present.

Step 3: Query returns matching documents

MongoDB returns all documents that have the specified field, ignoring documents where the field is missing.

🔄

Alternative Approaches

Check field existence with projection
mongodb
db.collection.find({}, { age: 1 })
This returns all documents but only shows the 'age' field if it exists; it does not filter documents.
Find documents where field does not exist
mongodb
db.collection.find({ age: { $exists: false } })
This finds documents where the field is missing instead of present.

Complexity: O(n) time, O(k) space

Time Complexity

The query scans each document in the collection to check if the field exists, so it is O(n) where n is the number of documents.

Space Complexity

The space used depends on the number of matching documents returned, O(k), where k is the count of documents with the field.

Which Approach is Fastest?

Using $exists is the most direct and efficient way to check field presence compared to filtering or projections.

ApproachTimeSpaceBest For
$exists: trueO(n)O(k)Filtering documents by field presence
Projection onlyO(n)O(n)Viewing specific fields without filtering
$exists: falseO(n)O(k)Finding documents missing a field
💡
Use $exists: true to quickly filter documents that have a specific field.
⚠️
Forgetting to use $exists and trying to check field presence with equality operators.