Challenge - 5 Problems
Nested Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2: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'?Attempts:
2 left
💡 Hint
Use dot notation to access nested fields in MongoDB queries.
✗ Incorrect
MongoDB uses dot notation to query nested fields. To find documents where the city inside address inside profile is 'New York', you must specify the full path 'profile.address.city'.
❓ query_result
intermediate2: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?Attempts:
2 left
💡 Hint
Dot notation works inside arrays to match nested fields in array elements.
✗ Incorrect
MongoDB matches array elements when you use dot notation. Querying 'items.product.details.category' matches any item in the items array whose product.details.category equals 'electronics'.
📝 Syntax
advanced2: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?Attempts:
2 left
💡 Hint
Field names with dots must be quoted as strings in MongoDB queries.
✗ Incorrect
In option C, the field name info.stats.score is not quoted as a string, which is invalid syntax in JavaScript object keys when they contain dots. This causes a syntax error.
❓ optimization
advanced2: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?Attempts:
2 left
💡 Hint
Indexes must match the exact field path used in queries.
✗ Incorrect
To optimize queries filtering on 'event.details.user.id', the index must be created on the exact nested field path 'event.details.user.id'. Other paths do not match and won't optimize the query.
🔧 Debug
expert3:00remaining
Why does this nested field query return no results?
A developer runs this query on collection
But it returns no documents, even though some documents have a mobile phone number under
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 } })Attempts:
2 left
💡 Hint
Dot notation does not traverse inside arrays automatically.
✗ Incorrect
If 'contact.phone' is an array, querying 'contact.phone.mobile' does not match nested fields inside array elements. You must use $elemMatch or other operators to query nested fields inside arrays.