Bird
Raised Fist0
MongoDBquery~20 mins

Array of embedded documents queries 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 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.

Practice

(1/5)
1.

Which MongoDB query operator is used to match documents where at least one element in an array of embedded documents meets multiple conditions?

easy
A. $exists
B. $all
C. $in
D. $elemMatch

Solution

  1. Step 1: Understand the need for multiple conditions on one embedded document

    When querying arrays of embedded documents, multiple conditions on the same element require a special operator.
  2. Step 2: Identify the correct operator for matching multiple conditions on one element

    $elemMatch matches documents where at least one array element satisfies all specified conditions.
  3. Final Answer:

    $elemMatch -> Option D
  4. Quick Check:

    Multiple conditions on one embedded doc = $elemMatch [OK]
Hint: Use $elemMatch for multiple conditions on one array element [OK]
Common Mistakes:
  • Using $all which matches all elements separately
  • Using $in which matches any value but not multiple conditions
  • Confusing $exists with array element matching
2.

Which of the following is the correct MongoDB query syntax to find documents where the comments array contains an embedded document with author equal to "Alice"?

{ comments: { ? } }
easy
A. { $in: { author: "Alice" } }
B. { author: "Alice" }
C. { $elemMatch: { author: "Alice" } }
D. { $all: { author: "Alice" } }

Solution

  1. Step 1: Identify simple query for matching one field in embedded documents

    To find documents where any embedded document in an array has a field equal to a value, use $elemMatch or direct matching.
  2. Step 2: Recognize that { comments: { $elemMatch: { author: "Alice" } } } matches if any embedded document has author "Alice"

    This syntax matches documents where at least one element in comments array has author "Alice".
  3. Final Answer:

    { $elemMatch: { author: "Alice" } } -> Option C
  4. Quick Check:

    Use $elemMatch for embedded document match [OK]
Hint: Use $elemMatch for embedded document match [OK]
Common Mistakes:
  • Using $in or $all incorrectly with embedded documents
  • Confusing array field with embedded document field
  • Using direct object without $elemMatch may not always work as expected
3.

Given the collection posts with documents like:

{ _id: 1, comments: [ { author: "Bob", likes: 5 }, { author: "Alice", likes: 3 } ] }

What will the query db.posts.find({ comments: { $elemMatch: { author: "Alice", likes: { $gt: 2 } } } }) return?

medium
A. [{ _id: 1, comments: [...] }]
B. [] (empty array)
C. Syntax error
D. All documents in posts collection

Solution

  1. Step 1: Understand $elemMatch with multiple conditions

    The query looks for documents where at least one comment has author "Alice" and likes greater than 2.
  2. Step 2: Check the example document for matching embedded document

    The second comment has author "Alice" and likes 3, which is greater than 2, so it matches.
  3. Final Answer:

    [{ _id: 1, comments: [...] }] -> Option A
  4. Quick Check:

    Matching embedded doc with $elemMatch returns document [OK]
Hint: Check all conditions inside $elemMatch for one embedded doc [OK]
Common Mistakes:
  • Assuming $elemMatch matches separate elements for each condition
  • Expecting empty result when conditions are met
  • Confusing $elemMatch with $all
4.

Identify the error in this MongoDB query to find documents where reviews array has an embedded document with rating greater than 4 and verified true:

db.collection.find({ reviews: { rating: { $gt: 4 }, verified: true } })
medium
A. reviews field should not be queried as an array
B. Missing $elemMatch to combine multiple conditions on one embedded document
C. Incorrect use of $gt operator
D. Syntax error due to missing commas

Solution

  1. Step 1: Recognize multiple conditions on one embedded document

    Multiple conditions on fields inside an array element require $elemMatch to ensure they apply to the same element.
  2. Step 2: Identify missing $elemMatch usage

    The query incorrectly tries to match multiple fields directly inside the array field, which matches documents with separate elements for each condition, not the same element.
  3. Final Answer:

    Missing $elemMatch to combine multiple conditions on one embedded document -> Option B
  4. Quick Check:

    Multiple conditions on one element need $elemMatch [OK]
Hint: Use $elemMatch for multiple conditions on same embedded doc [OK]
Common Mistakes:
  • Omitting $elemMatch for multiple conditions
  • Assuming direct object matches all conditions on one element
  • Misunderstanding array query behavior
5.

You have a collection orders with documents like:

{ _id: 1, items: [ { product: "Pen", qty: 10 }, { product: "Notebook", qty: 5 } ] }

Write a query to find orders where items contains a product "Pen" with quantity at least 10, and also a product "Notebook" with quantity at least 5.

Which query correctly finds such orders?

hard
A. { items: { $elemMatch: { product: "Pen", qty: { $gte: 10 } } }, items: { $elemMatch: { product: "Notebook", qty: { $gte: 5 } } } }
B. { items: { $elemMatch: { product: "Pen", qty: { $gte: 10 }, product: "Notebook", qty: { $gte: 5 } } } }
C. { items: { product: "Pen", qty: { $gte: 10 }, product: "Notebook", qty: { $gte: 5 } } }
D. { items: { $in: [ { product: "Pen", qty: { $gte: 10 } }, { product: "Notebook", qty: { $gte: 5 } } ] } }

Solution

  1. Step 1: Understand the need to match two different embedded documents in the array

    We want orders where items array has one element matching Pen with qty >= 10 and another element matching Notebook with qty >= 5.
  2. Step 2: Use repeated array field with $elemMatch to require both conditions on separate elements

    Repeating the array field key with $elemMatch for each condition ensures at least one array element satisfies each set of conditions.
  3. Step 3: Eliminate incorrect options

    { items: { $elemMatch: { product: "Pen", qty: { $gte: 10 }, product: "Notebook", qty: { $gte: 5 } } } } tries to match both products in one element (impossible). { items: { product: "Pen", qty: { $gte: 10 }, product: "Notebook", qty: { $gte: 5 } } } is invalid syntax. { items: { $in: [ { product: "Pen", qty: { $gte: 10 } }, { product: "Notebook", qty: { $gte: 5 } } ] } } uses $in which matches any element but not both conditions.
  4. Final Answer:

    { items: { $elemMatch: { product: "Pen", qty: { $gte: 10 } } }, items: { $elemMatch: { product: "Notebook", qty: { $gte: 5 } } } } -> Option A
  5. Quick Check:

    Repeated field + $elemMatch for multiple different embedded docs [OK]
Hint: Use repeated array field with $elemMatch for multiple different embedded docs [OK]
Common Mistakes:
  • Trying to match multiple products in one embedded document
  • Using $elemMatch alone for multiple different elements
  • Using $in which matches any but not all conditions