Thuta Learning
AdvancedData & Databasesintermediate

$match & $group

Relax. We'll talk through this in plain words — no textbook voice.

Commonly used stages in an aggregation pipeline:

$match: filters incoming documents (similar to find())

$group: groups documents by a specified key and computes totals ($sum), averages ($avg), and so on.

javascript
db.orders.aggregate([
   // Stage 1: Filter documents with status "A"
   { $match: { status: "A" } },
   
   // Stage 2: Group by customer_id and calculate total amount
   { $group: { _id: "$cust_id", total: { $sum: "$amount" } } }
])
You should see
(This will group orders with status "A" by each customer and calculate the total amount)
$match & $group | Thuta Learning