Solution: Multi-Stages Aggregation
We'll cover the following...
Query
db.orders.aggregate([{$match: {orderDate: {$gte: ISODate("2025-01-01T00:00:00Z"),$lt: ISODate("2026-01-01T00:00:00Z")}}},{$group: {_id: "$customerId",totalRevenue: { $sum: "$total" },orderCount: { $sum: 1 }}},{$match: {totalRevenue: { $gt: 0 }}}])
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:
$matchFilters the documents to include only orders placed ...