Open In App

Python MongoDB - $group (aggregation)

Last Updated : 02 Jul, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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.

Syntax

{
$group: {
_id: <grouping field>,
<newField>: { <accumulator>: <expression> }
}
}

Parameters:

  • _id: The field to group the documents by (e.g., group by "product name" or "category").
  • <newField>: The result field where you store the output (like total, average, etc.).
  • <accumulator>: The operation to perform on each group.
  • <expression>: The value to apply the operation on (e.g., the "amount" field).

Here is our sample data.

Python
from pymongo import MongoClient
c = MongoClient("mongodb://localhost:27017/")
db = c['grpDB']
col = db['sales']

data = [
    {"_id": 1, "user": "Amit", "product": "Pen", "amount": 5},
    {"_id": 2, "user": "Drew", "product": "Pencil", "amount": 3},
    {"_id": 3, "user": "Amit", "product": "Notebook", "amount": 15},
    {"_id": 4, "user": "Cody", "product": "Pen", "amount": 7},
    {"_id": 5, "user": "Drew", "product": "Notebook", "amount": 12},
    {"_id": 6, "user": "Cody", "product": "Eraser", "amount": 2},
    {"_id": 7, "user": "Amit", "product": "Pen", "amount": 10}
]
col.delete_many({})
col.insert_many(data)
print("Data inserted.")

Output

Data inserted.
Output
Data inserted

Explanation:

  • MongoClient() connects to the local server and selects grpDB.sales.
  • insert_many(data) loads sample orders into the sales collection.
  • delete_many({}) clears previous data to avoid duplicates.

Examples

Example 1: Average amount per user

Python
from pymongo import MongoClient

c = MongoClient("mongodb://localhost:27017/")
db = c['groupExampleDB']
collection = db['sales']

res = collection.aggregate([
    {
        "$group": {
            "_id": "$user",
            "average_amount": { "$avg": "$amount" }
        }
    }
])

for doc in res:
    print(doc)

Output

Output
User-wise average

Explanation:

  • $group groups documents by user.
  • $avg calculates average amount spent by each user.

Example 2: Total amount per product

Python
from pymongo import MongoClient
c = MongoClient("mongodb://localhost:27017/")
db = c['grpDB']
col = db['sales']

res = col.aggregate([
    {
        "$group": {
            "_id": "$product",
            "total_amount": { "$sum": "$amount" }
        }
    }
])
for doc in res:
    print(doc)

Output

Output
Product-wise total

Explanation:

  • $group organizes documents by product.
  • $sum computes the total sales amount for each product.

Example 3: Max amount spent per user

Python
from pymongo import MongoClient

c = MongoClient("mongodb://localhost:27017/")
db = c['grpDB']
col = db['sales']

res = col.aggregate([
    {
        "$group": {
            "_id": "$user",
            "max_spent": { "$max": "$amount" }
        }
    }
])
for doc in res:
    print(doc)

Output

Output
User-wise max spend

Explanation:

  • $group groups orders by user.
  • $max finds the highest amount spent by each user in a single transaction.

Related Articles


Next Article
Article Tags :
Practice Tags :

Similar Reads