You get the error “Exceeded memory limit for $group, but didn’t allow external sort.” How do you fix it?
The error "Exceeded memory limit for $group, but didn’t allow external sort" in MongoDB happens during an aggregation operation using the $group
stage when the operation consumes more memory than the default in-memory limit (100 MB).
How to Fix It
✅ 1. Allow Disk Use in Aggregation
The most straightforward fix is to enable disk use for the aggregation operation, which allows MongoDB to write temporary data to disk if memory is exceeded:
In the shell or drivers:
In Mongoose:
✅ 2. Reduce Data Before $group
Try to filter data earlier in the pipeline to reduce the amount processed by $group
:
✅ 3. Use Indexes Wisely
Sometimes a well-designed index can reduce the workload before $group
. For example, sorting or filtering before a group may benefit from covered indexes.
✅ 4. Group in Batches
If possible, break your operation into smaller batches and group each separately. For example, if grouping by date, you could group by month in separate jobs.
✅ 5. Check for Unbounded Grouping
Grouping by fields with high cardinality (e.g., user IDs or random values) can quickly blow past the memory limit. Make sure you're grouping only by fields that result in a manageable number of groups.
✅ 6. Upgrade MongoDB or Adjust Server Settings
If you're running on a very large dataset regularly, consider:
-
Upgrading to a newer version of MongoDB with better aggregation performance.
-
Scaling the hardware.
-
Sharding the collection (if you’re on a sharded cluster).
Summary: The quick fix is to pass { allowDiskUse: true }
in your aggregation. But for long-term performance and stability, it's wise to optimize your pipeline to reduce memory usage.