Challenge - 5 Problems
MongoDB $elemMatch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Find documents with a matching array element using $elemMatch
Given a collection
products where each document has an array field reviews with objects containing rating and comment, which query returns products that have at least one review with a rating greater than 4 and a comment containing the word 'excellent'?MongoDB
db.products.find({ reviews: { $elemMatch: { rating: { $gt: 4 }, comment: /excellent/i } } })Attempts:
2 left
💡 Hint
Use $elemMatch to match multiple conditions on the same array element.
✗ Incorrect
Option D correctly uses $elemMatch to find array elements where rating is greater than 4 and comment matches the regex 'excellent' case-insensitively. Option D is invalid because $elemMatch is missing. Option D looks for rating exactly 4 and comment exactly 'excellent' (case sensitive). Option D uses $gte 4 instead of $gt 4 and a regex without case-insensitive flag.
🧠 Conceptual
intermediate1:30remaining
Understanding $elemMatch behavior with multiple conditions
What does the $elemMatch operator do when used in a query on an array field?
Attempts:
2 left
💡 Hint
Think about how $elemMatch applies conditions to a single element.
✗ Incorrect
$elemMatch matches documents where at least one element in the array meets all the conditions inside it. It does not require all elements to match, nor does it match if conditions are split across different elements.
📝 Syntax
advanced1:30remaining
Identify the syntax error in $elemMatch usage
Which of the following queries will cause a syntax error when using $elemMatch in MongoDB?
Attempts:
2 left
💡 Hint
Check the brackets and object syntax inside $elemMatch.
✗ Incorrect
Option C is missing curly braces around the object passed to $elemMatch, causing a syntax error. The others have correct syntax.
❓ optimization
advanced2:00remaining
Optimizing queries with $elemMatch on large arrays
When querying documents with large arrays, which approach using $elemMatch is generally more efficient?
Attempts:
2 left
💡 Hint
Think about how MongoDB can use indexes and reduce scanning.
✗ Incorrect
Combining multiple conditions inside a single $elemMatch allows MongoDB to efficiently find array elements matching all conditions at once, improving performance especially on large arrays. Using multiple $elemMatch or $or can cause more scanning.
🔧 Debug
expert2:30remaining
Why does this $elemMatch query return no results?
Consider the collection
What is the most likely reason?
orders with documents containing an array items. Each item has productId and quantity. The query below returns no documents, but you expect some matches:db.orders.find({ items: { $elemMatch: { productId: 123, quantity: { $gt: 10 } } } })What is the most likely reason?
Attempts:
2 left
💡 Hint
Remember $elemMatch matches conditions on the same element, not across elements.
✗ Incorrect
Option B is correct because $elemMatch requires all conditions to be true on the same array element. If productId 123 is in one element and quantity > 10 in another, no match occurs. Option B could cause no matches but is less likely if data types are consistent. Option B is false; $elemMatch supports numeric comparisons. Option B would cause no matches but is less common.