0
0
MongoDBquery~20 mins

$elemMatch for complex array queries in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB $elemMatch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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 } } })
Adb.products.find({ reviews: { $elemMatch: { rating: { $gte: 4 }, comment: /excellent/ } } })
Bdb.products.find({ reviews: { rating: { $gt: 4 }, comment: /excellent/i } })
Cdb.products.find({ reviews: { $elemMatch: { rating: 4, comment: 'excellent' } } })
Ddb.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.
🧠 Conceptual
intermediate
1:30remaining
Understanding $elemMatch behavior with multiple conditions
What does the $elemMatch operator do when used in a query on an array field?
AIt matches documents where at least one array element satisfies all specified conditions inside $elemMatch.
BIt matches documents where all array elements satisfy each condition separately.
CIt matches documents where any array element satisfies at least one of the conditions inside $elemMatch.
DIt matches documents where the array contains exactly one element.
Attempts:
2 left
💡 Hint
Think about how $elemMatch applies conditions to a single element.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in $elemMatch usage
Which of the following queries will cause a syntax error when using $elemMatch in MongoDB?
Adb.collection.find({ items: { $elemMatch: { price: { $lt: 20 }, qty: { $gt: 5 } } } })
Bdb.collection.find({ items: { $elemMatch: { qty: { $gt: 5 } } } })
Cdb.collection.find({ items: { $elemMatch: price: { $lt: 20 }, qty: { $gt: 5 } } })
Ddb.collection.find({ items: { $elemMatch: { price: { $lt: 20 } } } })
Attempts:
2 left
💡 Hint
Check the brackets and object syntax inside $elemMatch.
optimization
advanced
2:00remaining
Optimizing queries with $elemMatch on large arrays
When querying documents with large arrays, which approach using $elemMatch is generally more efficient?
AUse $elemMatch to combine multiple conditions on the same array element instead of separate queries for each condition.
BUse multiple $elemMatch operators on the same array field for each condition separately.
CAvoid $elemMatch and use $or with separate conditions on the array field.
DUse $elemMatch only with a single condition to reduce complexity.
Attempts:
2 left
💡 Hint
Think about how MongoDB can use indexes and reduce scanning.
🔧 Debug
expert
2:30remaining
Why does this $elemMatch query return no results?
Consider the collection 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?
AThe quantity field is missing in all array elements.
BNo single array element has both productId 123 and quantity greater than 10; conditions are split across elements.
CThe $elemMatch operator cannot be used with numeric comparisons.
DThe productId field is stored as a string, but the query uses a number.
Attempts:
2 left
💡 Hint
Remember $elemMatch matches conditions on the same element, not across elements.