aggregate()
Reference group: Indexes and aggregation
What it does
Runs a pipeline to filter, reshape, join, and summarize documents.
Syntax
db.collection.aggregate(pipeline, options)
Use case
Reporting and analytics.
aggregate(): practical example
db.orders.aggregate([{ $match: { status: "paid" } }, { $group: { _id: "$customerId", spend: { $sum: "$total" } } }])💡 Explanation
Runs a pipeline to filter, reshape, join, and summarize documents.
When to use it
Reporting and analytics.
Key Takeaways
- Runs a pipeline to filter, reshape, join, and summarize documents.
- Typical use: Reporting and analytics.