Mongodb Certified Developer Associate C100dev · Free Practice Question Medium

Question 38

You have a MongoDB collection named orders that stores customer order details. Each document in the collection has a structure like this:



You need to retrieve all orders that contain at least one item with an item_id of "B200" and a quantity greater than or equal to 2. Which of the following queries would correctly retrieve the desired orders?

  • A
  • B
  • C
  • D
Reveal correct answer

Correct answer: D

A.

This query mistakenly tries to match items.item_id and items.quantity independently within the array. MongoDB treats these conditions as separate filters on the items array, which means it could match an item with item_id: "B200" and another item with quantity >= 2, but not necessarily the same item.

B.

The $all operator is used incorrectly here. $all is intended for matching multiple values within an array, but it is not designed to handle conditions across different fields within array elements. This query would not return the correct results and would likely result in an error.

C.

This query is syntactically incorrect because it attempts to apply conditions directly on an embedded document within an array without using $elemMatch. This would not return any results.

D.

The $elemMatch operator correctly specifies that both conditions—item_id: "B200" and quantity: { $gte: 2 }—must be true for the same element within the items array. This is the correct approach for ensuring that both conditions apply to a single item in the array.

Discussion

Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need