Solution: Multi-Stages Aggregation
The multi-stages aggregation solution in MongoDB processes documents from the orders collection through various stages. It begins by filtering orders from 2025 using the $match stage, followed by grouping these orders by customerId with the $group stage to calculate total revenue and order count. A second $match stage excludes customers with zero or negative revenue, ensuring only those with actual purchases are included in the results. Key concepts include filtering documents, grouping for aggregations, and using accumulator operators like $sum for calculations.
We'll cover the following...
The explanation of the query is given below:
Line 1: This starts the aggregation pipeline on the
orderscollection. Theaggregate()method processes documents through multiple stages to transform and analyze data.Lines 3–6:
$match