0
0
MongodbHow-ToBeginner · 2 min read

MongoDB Query to Find Nested Field Value

Use dot notation in your MongoDB query like { 'parent.child': value } to find documents where a nested field matches the value.
📋

Examples

Input{ 'address.city': 'New York' }
Output[{ '_id': 1, 'name': 'Alice', 'address': { 'city': 'New York', 'zip': '10001' } }]
Input{ 'profile.age': 30 }
Output[{ '_id': 2, 'profile': { 'age': 30, 'status': 'active' } }]
Input{ 'contact.email': 'user@example.com' }
Output[]
🧠

How to Think About It

To find a nested field value, think of the nested field as a path separated by dots. Use parent.child in the query to tell MongoDB to look inside the nested object for the value.
📐

Algorithm

1
Identify the nested field you want to search, e.g., 'parent.child'.
2
Write a query using dot notation with the nested field and the value to match.
3
Run the query to get documents where the nested field equals the value.
💻

Code

mongodb
db.users.find({ 'address.city': 'New York' })
Output
[ { _id: ObjectId("507f1f77bcf86cd799439011"), name: "Alice", address: { city: "New York", zip: "10001" } } ]
🔍

Dry Run

Let's trace the query { 'address.city': 'New York' } through the code

1

Identify nested field

Nested field is 'address.city' and value to find is 'New York'.

2

Query documents

MongoDB checks each document's 'address' object for 'city' equal to 'New York'.

3

Return matching documents

Documents with 'address.city' = 'New York' are returned.

Documentaddress.cityMatch
{ name: 'Alice', address: { city: 'New York', zip: '10001' } }New YorkYes
{ name: 'Bob', address: { city: 'Boston', zip: '02101' } }BostonNo
💡

Why This Works

Step 1: Dot notation access

Using parent.child tells MongoDB to look inside the nested object named parent for the field child.

Step 2: Matching value

MongoDB compares the nested field's value to the one given in the query and selects documents where they are equal.

🔄

Alternative Approaches

Using $elemMatch for nested arrays
mongodb
db.users.find({ 'orders': { $elemMatch: { 'item': 'book', 'qty': { $gt: 2 } } } })
Use this when nested fields are inside arrays and you want to match multiple conditions inside the same array element.
Using aggregation $match with $expr
mongodb
db.users.aggregate([{ $match: { $expr: { $eq: [ '$address.city', 'New York' ] } } }])
Aggregation allows more complex queries but is more verbose and less performant for simple nested field matches.

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

Time Complexity

The query scans documents to check the nested field, so time grows linearly with the number of documents.

Space Complexity

Only the matching documents are returned, so space depends on the result size, not the entire collection.

Which Approach is Fastest?

Simple dot notation queries are fastest for nested fields; aggregation is slower but more flexible.

ApproachTimeSpaceBest For
Dot notation queryO(n)O(k)Simple nested field matches
$elemMatch for arraysO(n)O(k)Matching multiple conditions in nested arrays
Aggregation $match with $exprO(n)O(k)Complex nested field conditions
💡
Use dot notation like 'parent.child' to query nested fields easily.
⚠️
Beginners often forget to use quotes around the nested field path or use incorrect dot notation.