0
0
MongoDBquery~20 mins

Querying array elements directly in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find documents with a specific array element
Given a collection products where each document has an array field tags, which query returns all documents where tags contains the string "organic"?
MongoDB
db.products.find({tags: "organic"})
Adb.products.find({tags: {$elemMatch: { $eq: "organic" }}})
Bdb.products.find({tags: "organic"})
Cdb.products.find({tags: {$in: ["organic"]}})
Ddb.products.find({tags: {$all: ["organic"]}})
Attempts:
2 left
💡 Hint
Check how MongoDB matches array elements directly by value.
query_result
intermediate
2:00remaining
Query documents with array elements matching a condition
Given a collection orders where each document has an array items with subdocuments containing price, which query returns documents where at least one item has a price greater than 100?
MongoDB
db.orders.find({"items.price": {$gt: 100}})
Adb.orders.find({items: {$elemMatch: {price: {$gt: 100}}}})
Bdb.orders.find({items: {$all: [{price: {$gt: 100}}]}})
Cdb.orders.find({items: {price: {$gt: 100}}})
Ddb.orders.find({"items.price": {$gt: 100}})
Attempts:
2 left
💡 Hint
Use dot notation to query array subdocuments directly.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in querying array elements
Which query will cause a syntax error when trying to find documents where the array scores contains a value less than 50?
Adb.students.find({scores: {$lt 50}})
Bdb.students.find({scores: {$elemMatch: {$lt: 50}}})
Cdb.students.find({scores: {$lt: 50}})
Ddb.students.find({"scores": {$lt: 50}})
Attempts:
2 left
💡 Hint
Check the syntax of the comparison operator inside the query object.
optimization
advanced
2:00remaining
Optimize query for array element existence
You want to find documents in users where the array roles contains the value "admin". Which query is the most efficient?
Adb.users.find({roles: "admin"})
Bdb.users.find({roles: {$elemMatch: {$eq: "admin"}}})
Cdb.users.find({roles: {$all: ["admin"]}})
Ddb.users.find({roles: {$in: ["admin"]}})
Attempts:
2 left
💡 Hint
Direct matching on array elements is usually fastest.
🧠 Conceptual
expert
3:00remaining
Understanding array element matching behavior
Consider a collection events where each document has an array participants with subdocuments containing name and age. Which query returns documents where there is a participant named "Alice" who is exactly 30 years old?
Adb.events.find({"participants.name": "Alice", "participants.age": 30})
Bdb.events.find({participants: {name: "Alice", age: 30}})
Cdb.events.find({participants: {$elemMatch: {name: "Alice", age: 30}}})
Ddb.events.find({participants: {$all: [{name: "Alice"}, {age: 30}]}})
Attempts:
2 left
💡 Hint
Use $elemMatch to match multiple conditions on the same array element.