Bird
Raised Fist0
MongoDBquery~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does the MongoDB query { tags: "mongodb" } do when applied to a collection where tags is an array field?
easy
A. Finds documents where the tags array contains the value "mongodb".
B. Finds documents where the tags array is exactly equal to "mongodb".
C. Finds documents where the tags array is empty.
D. Finds documents where the tags field does not exist.

Solution

  1. Step 1: Understand array field querying in MongoDB

    When querying an array field with a value, MongoDB checks if the array contains that value anywhere.
  2. Step 2: Analyze the query { tags: "mongodb" }

    This query looks for documents where the tags array includes the string "mongodb" as one of its elements.
  3. Final Answer:

    Finds documents where the tags array contains the value "mongodb". -> Option A
  4. Quick Check:

    Querying array with value checks for presence = B [OK]
Hint: Querying array with value checks for presence [OK]
Common Mistakes:
  • Thinking it matches exact array equality
  • Assuming it matches empty arrays
  • Confusing missing field with array content
2. Which of the following is the correct MongoDB query syntax to find documents where the array field scores contains the number 85?
easy
A. { scores: { $contains: 85 } }
B. { scores: { $in: 85 } }
C. { scores: 85 }
D. { scores: { $eq: [85] } }

Solution

  1. Step 1: Recall MongoDB syntax for matching array elements

    To find documents where an array contains a value, simply use { field: value } syntax.
  2. Step 2: Evaluate each option

    { scores: { $contains: 85 } } uses a non-existent operator $contains. { scores: { $eq: [85] } } incorrectly uses $eq with an array. { scores: { $in: 85 } } uses $in incorrectly without an array. { scores: 85 } correctly uses { scores: 85 }.
  3. Final Answer:

    { scores: 85 } -> Option C
  4. Quick Check:

    Simple value match syntax = D [OK]
Hint: Use {field: value} to match array elements directly [OK]
Common Mistakes:
  • Using non-existent operators like $contains
  • Misusing $eq with arrays
  • Passing non-array to $in operator
3. Given the collection documents:
{ _id: 1, scores: [70, 85, 90] }
{ _id: 2, scores: [60, 75] }
{ _id: 3, scores: [85, 95] }

What will be the result of the query { scores: 85 }?
medium
A. [] (empty array)
B. [{ _id: 2, scores: [60, 75] }]
C. Syntax error
D. [{ _id: 1, scores: [70, 85, 90] }, { _id: 3, scores: [85, 95] }]

Solution

  1. Step 1: Identify which documents have 85 in their scores array

    Document 1 has scores [70, 85, 90] which includes 85. Document 3 has scores [85, 95] which also includes 85. Document 2 does not have 85.
  2. Step 2: Understand the query result

    The query { scores: 85 } returns all documents where the scores array contains 85, so documents 1 and 3.
  3. Final Answer:

    [{ _id: 1, scores: [70, 85, 90] }, { _id: 3, scores: [85, 95] }] -> Option D
  4. Quick Check:

    Documents with 85 in scores = C [OK]
Hint: Query returns docs where array contains value [OK]
Common Mistakes:
  • Expecting only one document
  • Thinking query returns empty if multiple matches
  • Confusing syntax error with valid query
4. Consider this incorrect MongoDB query to find documents where the tags array contains both "red" and "blue":
{ tags: { $all: "red", "blue" } }

What is the main issue with this query?
medium
A. The $all operator requires an array of values, not separate arguments.
B. The query should use $elemMatch instead of $all.
C. The field name should be inside quotes.
D. The query is missing a $and operator.

Solution

  1. Step 1: Understand $all operator syntax

    The $all operator expects an array of values to match all elements inside the array field.
  2. Step 2: Identify the syntax error in the query

    The query incorrectly passes separate arguments to $all instead of an array. Correct syntax is { tags: { $all: ["red", "blue"] } }.
  3. Final Answer:

    The $all operator requires an array of values, not separate arguments. -> Option A
  4. Quick Check:

    $all needs array syntax = A [OK]
Hint: Use array syntax with $all operator [OK]
Common Mistakes:
  • Passing multiple values without array brackets
  • Confusing $all with $elemMatch
  • Ignoring syntax errors in operator usage
5. You want to find documents where the ratings array contains at least one element greater than 4 and less than 7. Which query correctly uses $elemMatch to achieve this?
hard
A. { ratings: { $gt: 4, $lt: 7 } }
B. { ratings: { $elemMatch: { $gt: 4, $lt: 7 } } }
C. { ratings: { $in: [5, 6] } }
D. { ratings: { $elemMatch: { $gte: 4, $lte: 7 } } }

Solution

  1. Step 1: Understand $elemMatch usage for multiple conditions on array elements

    $elemMatch allows specifying multiple conditions that must be true for the same array element.
  2. Step 2: Analyze each option for correctness

    { ratings: { $elemMatch: { $gt: 4, $lt: 7 } } } correctly uses $elemMatch with $gt and $lt to find elements >4 and <7. { ratings: { $gt: 4, $lt: 7 } } is invalid syntax because $gt and $lt cannot be used directly on the array field. { ratings: { $in: [5, 6] } } matches specific values but does not cover the range condition. { ratings: { $elemMatch: { $gte: 4, $lte: 7 } } } uses $gte and $lte which includes 4 and 7, not strictly greater and less.
  3. Final Answer:

    { ratings: { $elemMatch: { $gt: 4, $lt: 7 } } } -> Option B
  4. Quick Check:

    Use $elemMatch for multiple conditions on one element = A [OK]
Hint: Use $elemMatch for multiple conditions on array elements [OK]
Common Mistakes:
  • Using $gt and $lt directly on array field
  • Using $in instead of range operators
  • Confusing inclusive and exclusive range operators