Thuta Learning
MongoDB
AdvancedData & Databasesintermediate

$match & $group

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