How to speed up $group phase in aggregation?

To speed up the $group phase in MongoDB aggregation, optimize your pipeline by reducing the dataset size before grouping and using indexes effectively. The key is to filter, project, and limit data early in the pipeline.

Syntax

db.collection.aggregate([
    { $match: { /* filter conditions */ } },
    { $project: { /* only needed fields */ } },
    { $unwind: "$arrayField" },
    { $group: { _id: "$field", count: { $sum: 1 } } },
    { $sort: { count: -1 } },
    { $limit: n }
]);

Sample Data

db.demo423.insertOne({
    "Information": [101, 110, 87, 110, 98, 115, 101, 115, 89, 115]
});
{
    "acknowledged": true,
    "insertedId": ObjectId("5e73a60e9822da45b30346e6")
}

Example: Optimized $group Pipeline

Find the top 2 most frequent values in the Information array ?

db.demo423.aggregate([
    {
        $project: { _id: 0, "Information": 1 }
    },
    {
        $unwind: "$Information"
    },
    {
        $group: { _id: "$Information", frequency: { $sum: 1 } }
    },
    {
        $sort: { frequency: -1 }
    },
    {
        $limit: 2
    }
]);
{ "_id": 115, "frequency": 3 }
{ "_id": 110, "frequency": 2 }

Key Optimization Techniques

  • $project early: Remove unnecessary fields before $group to reduce memory usage
  • $match first: Filter documents early to minimize data processed by $group
  • Use indexes: Ensure fields used in $match are indexed
  • $limit after $sort: Reduces final result set size efficiently

Conclusion

Speed up $group operations by filtering and projecting data early, using appropriate indexes, and limiting results. This reduces memory usage and processing time in the aggregation pipeline.

Updated on: 2026-03-15T02:55:59+05:30

210 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements