Mongodb Certified Developer Associate C100dev · Free Practice Question Medium

Question 35

You are managing a MongoDB collection named orders, where each document represents an order. The documents contain the following fields:

  • order_id (String): The unique identifier for the order.

  • customer_name (String): The name of the customer who placed the order.

  • total_amount (Number): The total amount of the order.

  • status (String): The status of the order, which can be "pending," "shipped," or "delivered."

  • items (Array of Strings): A list of items included in the order.


You want to find all orders that meet the following criteria:

  1. The order has a total amount greater than $1000.

  2. The order status is either "pending" or "shipped."

  3. The order contains either "laptop" or "smartphone" in the list of items.


Which of the following queries would return the correct documents?

  • A
  • B
  • C
  • D
Reveal correct answer

Correct answer: B

A.

This query is incorrect because it uses $or at the top level, which means that any of the conditions could be met independently. This would result in orders that meet only one of the three criteria being returned, which is not the desired outcome.

B.

This query is correctly structured, using $and to ensure all conditions must be met and $or to handle the flexibility in status and items fields. The $in operator correctly checks if the items array contains either "laptop" or "smartphone."

C.

This query mixes $and and $or incorrectly by placing the items condition outside the $and block. As a result, it could cause logical errors or unexpected behavior, leading to incorrect results being returned.

D.

This query uses the $all operator incorrectly. A common misconception is that $all behaves like $in, but it’s important to note that $all requires all listed elements to be present in the array, whereas $in only requires that at least one of the elements is present. Therefore, $in should be used instead of $all when you want to match any one of the elements, which is the correct interpretation of the scenario.

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