Challenge - 5 Problems
Array Embedded Docs Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Find documents with an embedded array containing a specific value
Given a collection
students where each document has an array field grades with embedded documents like { subject: "Math", score: 85 }, which query returns all students who have a Math score greater than 90?MongoDB
db.students.find({ grades: { $elemMatch: { subject: "Math", score: { $gt: 90 } } } })Attempts:
2 left
💡 Hint
Use $elemMatch to match multiple conditions inside the same embedded document in the array.
✗ Incorrect
Option A correctly uses $elemMatch to find array elements where both conditions (subject is 'Math' and score > 90) are true in the same embedded document. Option A matches documents where any grade has subject 'Math' and any grade has score > 90, but not necessarily in the same embedded document. Option A is invalid syntax because $elemMatch is missing. Option A uses $all incorrectly for embedded documents.
❓ query_result
intermediate2:00remaining
Count documents with an embedded array matching a condition
In a
products collection, each document has an array reviews with embedded documents like { user: "Alice", rating: 5 }. Which query counts how many products have at least one review with rating 5?MongoDB
db.products.countDocuments({ reviews: { $elemMatch: { rating: 5 } } })Attempts:
2 left
💡 Hint
Use $elemMatch to ensure the rating 5 is in the same embedded document.
✗ Incorrect
Option B correctly counts documents where at least one embedded review has rating 5 using $elemMatch. Option B matches documents where any review has rating 5 but may count duplicates or not be precise for embedded docs. Option B is invalid syntax. Option B misuses $all for embedded documents.
📝 Syntax
advanced2:00remaining
Identify the syntax error in this embedded document query
Which option contains a syntax error when querying documents with an embedded array
items where each item has name and qty fields, to find documents with an item named 'apple' and quantity greater than 10?MongoDB
db.orders.find({ items: { $elemMatch: { name: 'apple', qty: { $gt: 10 } } } })Attempts:
2 left
💡 Hint
Check how operators like $gt are used inside queries.
✗ Incorrect
Option D is invalid because $gt must be inside an object: { $gt: 10 }, not qty: $gt: 10. Options A, B, and C are syntactically correct queries.
❓ optimization
advanced2:00remaining
Optimize query to find documents with multiple conditions on embedded array
You want to find documents in
orders where the products array contains an item with category 'electronics' and price less than 100. Which query is the most efficient and correct?Attempts:
2 left
💡 Hint
Use $elemMatch to ensure both conditions apply to the same embedded document.
✗ Incorrect
Option C uses $elemMatch to match both conditions in the same embedded document efficiently. Option C matches documents where any product has category 'electronics' and any product has price < 100, but not necessarily the same product. Option C misuses $all for embedded documents. Option C is invalid syntax.
🧠 Conceptual
expert3:00remaining
Understanding the difference between $elemMatch and dot notation in embedded array queries
Which statement best explains the difference between querying with
{ 'comments.author': 'John', 'comments.likes': { $gt: 10 } } and { comments: { $elemMatch: { author: 'John', likes: { $gt: 10 } } } } on a collection where comments is an array of embedded documents?Attempts:
2 left
💡 Hint
Think about whether conditions apply to the same embedded document or different ones.
✗ Incorrect
Option A correctly explains that dot notation conditions can match different elements in the array, while $elemMatch ensures all conditions apply to the same embedded document. Option A is incorrect because results can differ. Option A is incorrect about performance assumptions. Option A misrepresents the behavior of $elemMatch and dot notation.