Search⌘ K
AI Features

Aggregation Stages

Explore key aggregation stages in MongoDB to build powerful query pipelines. Learn how to filter with $match, sort with $sort, customize output with $project, and group data using $group. Master these stages to perform advanced data analysis and gain deeper insights into your collections.

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.

MongoDB
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 ...