The MongoDB aggregation framework is a powerful tool that allows you to perform complex operations on your data. It is based on the concept of pipelines, which are a series of stages that are applied to your data in sequence. Each stage performs an operation on the data, and the output of one stage is used as the input for the next stage.
The aggregation framework provides a wide range of stages that can be used to perform a variety of operations, including:
Filtering: The $match stage can be used to filter the data based on a condition.
Grouping: The $group stage can be used to group the data by a field and perform aggregation operations on the grouped data.
Calculating: The $sum, $avg, $min, and $max stages can be used to calculate summary statistics on the data.
Sorting: The $sort stage can be used to sort the data by a field.
Projecting: The $project stage can be used to project the data to include only the desired fields.
Creating documents: The $addFields stage can be used to add new fields to the data.
To use the aggregation framework, you first need to create an aggregation pipeline. An aggregation pipeline is a JSON object that specifies the stages that are to be applied to the data. The following is an example of an aggregation pipeline that filters the data by the age field and then groups the data by the country field:
To execute an aggregation pipeline, you use the aggregate() method on a collection object. The following is an example of how to execute the aggregation pipeline that was defined above:
The aggregation framework is a powerful tool that can be used to perform complex operations on your data. It is a great way to get more value out of your data and to gain insights that you would not be able to get with simple queries.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
The MongoDB aggregation framework is a powerful tool that allows you to perform complex operations on your data. It is based on the concept of pipelines, which are a series of stages that are applied to your data in sequence. Each stage performs an operation on the data, and the output of one stage is used as the input for the next stage.
The aggregation framework provides a wide range of stages that can be used to perform a variety of operations, including:
To use the aggregation framework, you first need to create an aggregation pipeline. An aggregation pipeline is a JSON object that specifies the stages that are to be applied to the data. The following is an example of an aggregation pipeline that filters the data by the age field and then groups the data by the country field:
Code snippet
To execute an aggregation pipeline, you use the aggregate() method on a collection object. The following is an example of how to execute the aggregation pipeline that was defined above:
Code snippet
The aggregation framework is a powerful tool that can be used to perform complex operations on your data. It is a great way to get more value out of your data and to gain insights that you would not be able to get with simple queries.