...

/

Aggregation Stages

Aggregation Stages

Explore key MongoDB aggregation pipeline stages—$match, $group, $sort, and $project—used for filtering, grouping, sorting, and reshaping data to perform complex data analysis and transformation.

MongoDB offers some keywords to add stages in our pipelines to perform required actions. Let’s see each one in detail.

The $match stage

It filters the documents and is very similar to the .find() method. Here's an example used to filter documents in the orders collection to include only shipped orders.

Press + to interact
MongoDB
Files
db.orders.aggregate([
{
$match: {
status: "Shipped"
}
}
])

Explanation: In line 1, we directly specify that this query needs to be executed via the aggregation pipeline: db.orders.aggregate. Here, $match tells MongoDB to only include documents where the status field is exactly ...