0
0
MongoDBquery~20 mins

Array of embedded documents queries in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Embedded Docs Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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 } } } })
Adb.students.find({ grades: { $elemMatch: { subject: "Math", score: { $gt: 90 } } } })
Bdb.students.find({ grades: { subject: "Math", score: { $gt: 90 } } })
Cdb.students.find({ "grades.subject": "Math", "grades.score": { $gt: 90 } })
Ddb.students.find({ grades: { $all: [ { subject: "Math" }, { score: { $gt: 90 } } ] } })
Attempts:
2 left
💡 Hint
Use $elemMatch to match multiple conditions inside the same embedded document in the array.
query_result
intermediate
2: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 } } })
Adb.products.countDocuments({ reviews: { $all: [ { rating: 5 } ] } })
Bdb.products.countDocuments({ reviews: { $elemMatch: { rating: 5 } } })
Cdb.products.countDocuments({ reviews: { rating: 5 } })
Ddb.products.countDocuments({ "reviews.rating": 5 })
Attempts:
2 left
💡 Hint
Use $elemMatch to ensure the rating 5 is in the same embedded document.
📝 Syntax
advanced
2: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 } } } })
Adb.orders.find({ items: { $elemMatch: { name: 'apple', qty: { $gt: 10 } } } })
Bdb.orders.find({ items: { $elemMatch: { name: 'apple', qty: { $gte: 10 } } } })
Cdb.orders.find({ 'items.name': 'apple', 'items.qty': { $gt: 10 } })
Ddb.orders.find({ items: { $elemMatch: { name: 'apple', qty: $gt: 10 } } })
Attempts:
2 left
💡 Hint
Check how operators like $gt are used inside queries.
optimization
advanced
2: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?
Adb.orders.find({ products: { $all: [ { category: 'electronics' }, { price: { $lt: 100 } } ] } })
Bdb.orders.find({ 'products.category': 'electronics', 'products.price': { $lt: 100 } })
Cdb.orders.find({ products: { $elemMatch: { category: 'electronics', price: { $lt: 100 } } } })
Ddb.orders.find({ products: { category: 'electronics', price: { $lt: 100 } } })
Attempts:
2 left
💡 Hint
Use $elemMatch to ensure both conditions apply to the same embedded document.
🧠 Conceptual
expert
3: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?
AThe first query matches documents where any comment has author 'John' and any comment has likes > 10, possibly in different comments; the second query matches documents where the same comment has both author 'John' and likes > 10.
BBoth queries always return the same documents because they check the same conditions on the comments array.
CThe first query is faster because it uses dot notation; the second query is slower because $elemMatch is complex.
DThe second query matches documents where all comments have author 'John' and likes > 10; the first query matches if any comment matches either condition.
Attempts:
2 left
💡 Hint
Think about whether conditions apply to the same embedded document or different ones.