0
0
MongoDBquery~20 mins

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

Choose your learning style9 modes available
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.