0
0
MongoDBquery~10 mins

$elemMatch for complex array queries in MongoDB - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to find documents where the 'scores' array has an element greater than 80.

MongoDB
db.students.find({ scores: { [1]: { score: { $gt: 80 } } } })
Drag options to blanks, or click blank then click option'
A$and
B$gt
C$elemMatch
D$in
Attempts:
3 left
💡 Hint
Common Mistakes
Using $gt directly on the array field instead of inside $elemMatch.
Using $in which checks for exact values, not conditions.
2fill in blank
medium

Complete the code to find documents where the 'grades' array has an element with 'score' greater than 85 and 'type' equal to 'exam'.

MongoDB
db.records.find({ grades: { $elemMatch: { score: { [1]: 85 }, type: 'exam' } } })
Drag options to blanks, or click blank then click option'
A$gt
B$lt
C$eq
D$ne
Attempts:
3 left
💡 Hint
Common Mistakes
Using $lt which means less than.
Using $eq which means equal to.
3fill in blank
hard

Fix the error in the query to find documents where 'items' array has an element with 'price' less than 50 and 'qty' greater than 10.

MongoDB
db.orders.find({ items: { [1]: { price: { $lt: 50 }, qty: { $gt: 10 } } } })
Drag options to blanks, or click blank then click option'
A$and
B$or
C$all
D$elemMatch
Attempts:
3 left
💡 Hint
Common Mistakes
Using $and or $or directly on the array field instead of $elemMatch.
Using $all which matches all elements but not conditions inside elements.
4fill in blank
hard

Fill both blanks to find documents where 'reviews' array has an element with 'rating' at least 4 and 'verified' is true.

MongoDB
db.products.find({ reviews: { [1]: { rating: { [2]: 4 }, verified: true } } })
Drag options to blanks, or click blank then click option'
A$elemMatch
B$gte
C$lt
D$ne
Attempts:
3 left
💡 Hint
Common Mistakes
Using $lt instead of $gte for rating condition.
Using $ne which means not equal.
5fill in blank
hard

Fill all three blanks to find documents where 'logs' array has an element with 'status' equal to 'error', 'code' greater than 500, and 'retry' false.

MongoDB
db.system.find({ logs: { [1]: { status: [2], code: { [3]: 500 }, retry: false } } })
Drag options to blanks, or click blank then click option'
A$elemMatch
B'error'
C$gt
D'success'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $and instead of $elemMatch.
Using 'success' instead of 'error' for status.
Using $lt instead of $gt for code.