Mongodb Certified Developer Associate C100dev · Free Practice Question Hard

Question 42

You are designing an aggregation pipeline to retrieve documents from the orders collection. Your goal is to calculate the total sales amount per customer by multiplying price and quantity in each document, summing the results, and returning a list sorted by total in descending order. Which pipeline stages and order would best fulfill this requirement?

  • A
  • B
  • C
  • D
Reveal correct answer

Correct answer: A

A.

This approach first calculates a line total per document (price * quantity) using $project, then groups by customerId to sum totals per customer, and finally sorts them. This is the correct and complete pipeline.

B.

$add is used incorrectly; adding price + quantity makes no logical sense in calculating a total cost. The intention is to multiply, not add.

C.

This pipeline sums only the quantity, not the actual value of the order. Also, the $sort before $group is not meaningful in this context and doesn’t contribute to the correct result.

D.

This pipeline only sums the price field directly, ignoring quantity. This does not compute the sales amount per item (price * quantity), hence the final total is inaccurate.

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