Thuta Learning
MongoDB
AdvancedData & DatabasesIntermediate

$match & $group

MongoDBLesson 17

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)

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

$match & $group | Thuta Learning