Bird
Raised Fist0
MongoDBquery~20 mins

Querying nested fields at any depth in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Nested Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find documents with a nested field value
Given a collection users where each document may have a nested field profile.address.city, which query returns all users living in 'New York'?
A{ 'profile.city': 'New York' }
B{ 'profile.address.city': 'New York' }
C{ 'address.city': 'New York' }
D{ 'profile.address': 'New York' }
Attempts:
2 left
💡 Hint
Use dot notation to access nested fields in MongoDB queries.
query_result
intermediate
2:00remaining
Query nested arrays with dot notation
In a collection orders, each document has an array field items where each item has a nested field product.details.category. Which query finds orders containing at least one item in the 'electronics' category?
A{ 'items.product.category': 'electronics' }
B{ 'items.category': 'electronics' }
C{ 'product.details.category': 'electronics' }
D{ 'items.product.details.category': 'electronics' }
Attempts:
2 left
💡 Hint
Dot notation works inside arrays to match nested fields in array elements.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in nested field query
Which of the following MongoDB queries will cause a syntax error when trying to find documents with nested field info.stats.score greater than 50?
A{ 'info.stats.score': { $gt: 50 } }
B{ 'info.stats.score': { '$gt': 50 } }
C{ info.stats.score: { $gt: 50 } }
D} } 05 :tg$ { :'erocs.stats.ofni' {
Attempts:
2 left
💡 Hint
Field names with dots must be quoted as strings in MongoDB queries.
optimization
advanced
2:00remaining
Optimize query on deeply nested fields
You have a large collection logs with documents containing nested field event.details.user.id. Which index will optimize queries filtering by this nested user id?
A{ 'event.details.user.id': 1 }
B{ 'event.user.id': 1 }
C{ 'details.user.id': 1 }
D{ 'user.id': 1 }
Attempts:
2 left
💡 Hint
Indexes must match the exact field path used in queries.
🔧 Debug
expert
3:00remaining
Why does this nested field query return no results?
A developer runs this query on collection profiles:
db.profiles.find({ 'contact.phone.mobile': { $exists: true } })
But it returns no documents, even though some documents have a mobile phone number under contact.phone.mobile. What is the most likely reason?
MongoDB
db.profiles.find({ 'contact.phone.mobile': { $exists: true } })
ASome documents have <code>contact.phone</code> as an array, so dot notation does not match nested fields inside array elements.
BThe field <code>contact.phone.mobile</code> is misspelled; it should be <code>contact.mobile.phone</code>.
CThe query syntax is invalid because $exists cannot be used with nested fields.
DThe collection <code>profiles</code> is empty, so no documents are returned.
Attempts:
2 left
💡 Hint
Dot notation does not traverse inside arrays automatically.

Practice

(1/5)
1. Which MongoDB query syntax correctly accesses a nested field named address.city inside a document?
easy
A. { address.city() : "New York" }
B. { address: { city: "New York" } }
C. { "address->city": "New York" }
D. { "address.city": "New York" }

Solution

  1. Step 1: Understand dot notation for nested fields

    MongoDB uses dot notation like "address.city" to access nested fields inside documents.
  2. Step 2: Identify correct query syntax

    The correct query uses a string key with dot notation: { "address.city": "New York" }.
  3. Final Answer:

    { "address.city": "New York" } -> Option D
  4. Quick Check:

    Dot notation = { "address.city": value } [OK]
Hint: Use quotes and dot notation to access nested fields [OK]
Common Mistakes:
  • Using nested objects instead of dot notation in query
  • Using arrow or parentheses instead of dot notation
  • Not quoting the nested field path
2. Which of the following is the correct MongoDB query to find documents where the nested field profile.details.age equals 30?
easy
A. { profile.details.age = 30 }
B. { "profile.details.age": 30 }
C. { profile.details.age: 30 }
D. { 'profile.details.age' == 30 }

Solution

  1. Step 1: Use string keys with dot notation in MongoDB queries

    MongoDB requires the nested field path as a string key with dots, like "profile.details.age".
  2. Step 2: Use colon for key-value pairs in query objects

    The correct syntax uses colon (:), not equals (=) or double equals (==), so { "profile.details.age": 30 } is correct.
  3. Final Answer:

    { "profile.details.age": 30 } -> Option B
  4. Quick Check:

    Dot notation with colon = correct query [OK]
Hint: Use colon and quotes for nested keys in queries [OK]
Common Mistakes:
  • Using = or == instead of colon in query
  • Not quoting the nested field path
  • Using object notation instead of dot notation
3. Given the collection documents:
{ "user": { "contact": { "email": "a@example.com" } } }, { "user": { "contact": { "email": "b@example.com" } } }, { "user": { "contact": { "phone": "12345" } } }

What will the query db.collection.find({ "user.contact.email": { $exists: true } }) return?
medium
A. Documents where user.contact has any field
B. Documents where user.contact.phone exists
C. Documents where user.contact.email exists
D. All documents in the collection

Solution

  1. Step 1: Understand $exists operator with nested fields

    The query checks if the nested field "user.contact.email" exists in documents.
  2. Step 2: Identify which documents have that nested field

    Only the first two documents have "user.contact.email"; the third has "user.contact.phone" instead.
  3. Final Answer:

    Documents where user.contact.email exists -> Option C
  4. Quick Check:

    $exists true filters documents with that nested field [OK]
Hint: Use $exists to check nested field presence [OK]
Common Mistakes:
  • Assuming $exists checks parent fields
  • Confusing email and phone fields
  • Expecting all documents to match
4. You wrote this query to find documents where profile.address.zipcode is "12345":
db.users.find({ profile.address.zipcode: "12345" })

But it gives a syntax error. What is the fix?
medium
A. Use quotes around the nested field: { "profile.address.zipcode": "12345" }
B. Replace dots with underscores: { profile_address_zipcode: "12345" }
C. Use double equals: { "profile.address.zipcode" == "12345" }
D. Remove the nested field and query only { zipcode: "12345" }

Solution

  1. Step 1: Identify syntax error cause

    MongoDB requires string keys with dot notation quoted in queries to avoid syntax errors.
  2. Step 2: Correct query syntax

    Wrap the nested field path in quotes: { "profile.address.zipcode": "12345" } fixes the syntax error.
  3. Final Answer:

    Use quotes around the nested field: { "profile.address.zipcode": "12345" } -> Option A
  4. Quick Check:

    Quotes fix dot notation syntax errors [OK]
Hint: Always quote nested field keys in queries [OK]
Common Mistakes:
  • Not quoting nested field keys
  • Using == instead of colon
  • Replacing dots with underscores incorrectly
5. You have documents with deeply nested fields like settings.preferences.notifications.email.enabled. How would you write a MongoDB query to find all documents where email notifications are enabled (true), regardless of nesting depth?
hard
A. { "settings.preferences.notifications.email.enabled": true }
B. { settings: { preferences: { notifications: { email: { enabled: true } } } } }
C. { "email.enabled": true }
D. { "settings.preferences.notifications.email.enabled": { $eq: "true" } }

Solution

  1. Step 1: Use dot notation string to access deeply nested field

    MongoDB queries use dot notation strings to reach any depth, so "settings.preferences.notifications.email.enabled" is correct.
  2. Step 2: Match boolean value correctly

    Use true (boolean) without quotes to match enabled field, so { "settings.preferences.notifications.email.enabled": true } is correct.
  3. Final Answer:

    { "settings.preferences.notifications.email.enabled": true } -> Option A
  4. Quick Check:

    Dot notation + boolean value = correct query [OK]
Hint: Use full dot path with boolean true (no quotes) [OK]
Common Mistakes:
  • Using nested objects instead of dot notation
  • Quoting boolean true as string
  • Using partial nested paths