![]() |
VOOZH | about |
In PyMongo, the aggregate() method processes data through a pipeline of stages to produce aggregated results. One key stage is $group, which groups documents based on a specified identifier like a field name and applies accumulator operations e.g., $sum, $avg, $max. It then outputs a new set of documents representing the grouped and processed data.
{
$group: {
_id: <grouping field>,
<newField>: { <accumulator>: <expression> }
}
}
Parameters:
Here is our sample data.
Output
Data inserted.Explanation:
Example 1: Average amount per user
Output
Explanation:
Example 2: Total amount per product
Output
Explanation:
Example 3: Max amount spent per user
Output
Explanation:
Related Articles