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 all users with a nested address in New York
Given a collection
users where each document has a nested address field with city inside, which query returns all users living in New York?MongoDB
db.users.find({"address.city": "New York"})Attempts:
2 left
💡 Hint
Think about how to access nested fields using dot notation.
✗ Incorrect
To query nested fields in MongoDB, you use dot notation like "address.city". Options B, C, and D either look for a top-level field or an exact match of the nested object, which won't match documents where address has other fields.
🧠 Conceptual
intermediate1:30remaining
Why is querying nested data important?
Why do databases like MongoDB allow querying nested data directly instead of flattening all data?
Attempts:
2 left
💡 Hint
Think about how real-world data is structured and how grouping related info helps.
✗ Incorrect
Nested data groups related information in one place, so queries can target specific parts without joining multiple tables or collections. This makes queries simpler and often faster.
📝 Syntax
advanced2:00remaining
Correct syntax to query nested array elements
Which MongoDB query correctly finds documents where the nested array
orders contains an item with product equal to "Book"?MongoDB
db.customers.find({"orders.product": "Book"})Attempts:
2 left
💡 Hint
Use dot notation to query inside arrays of objects.
✗ Incorrect
Option B uses dot notation to check if any element in the orders array has product "Book". Options A and C look for exact matches of the nested object, which won't work. Option B only matches if the first element (index 0) of the orders array has product "Book".
❓ optimization
advanced2:30remaining
Improving query performance on nested fields
You have a large collection with nested documents. Which approach improves query speed when filtering by a nested field?
Attempts:
2 left
💡 Hint
Indexes help databases find data faster.
✗ Incorrect
Creating an index on the nested field (using dot notation) allows MongoDB to quickly locate documents matching the query. Other options either avoid indexes or add overhead, slowing queries.
🔧 Debug
expert3:00remaining
Why does this nested query return no results?
Given this query:
db.products.find({"details.specs.weight": { $gt: 10 }}), it returns no results even though some products have weight greater than 10. What is the most likely reason?Attempts:
2 left
💡 Hint
Check the data type of the nested field you are comparing.
✗ Incorrect
If "weight" is stored as a string like "15" instead of a number, $gt will not work as expected because it compares numbers, not strings. Dot notation and $gt work fine on nested fields if data types match.