Main Tutorials

MongoDB : Sort exceeded memory limit of 104857600 bytes

Performs a large sort operation (Aggregation) in a collection, and hits the following error message :

MongoDB Console

> db.bigdata.aggregate(
	{$group : {_id : "$range", total : { $sum : 1 }}},
	{$sort : {total : -1}}
);

#...
 aggregate failed
    at Error (<anonymous>)
    at doassert (src/mongo/shell/assert.js:11:14)
    #...
    Error: command failed: {
        "errmsg" : "exception: Sort exceeded memory limit of 104857600 bytes, 
		  but did not opt in to external sorting. Aborting operation. Pass allowDiskUse:true to opt in.",
        "code" : 16819,
        "ok" : 0
	}

P.S Tested with MongoDB 3.0.6

Solution

Changed in version 2.6 – Read this Memory Restrictions
In MongoDB, the in-memory sorting have a limit of 100M, to perform a large sort, you need enable allowDiskUse option to write data to a temporary files for sorting.

To fix it, enable the allowDiskUse option in your query :


db.bigdata.aggregate(
[
	{$group : {_id : "$range", total : { $sum : 1 }}},
	{$sort : {total : -1}}
],
	{allowDiskUse: true}
);

References

  1. Perform Large Sort Operation with External Sort
  2. Aggregation Pipeline Limits
  3. MongoDB – Aggregate and Group example

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Vincensius Adi N
8 years ago

Hi mkyong,

I got thi error :

“allowDiskUse” : true}; nested exception is com.mongodb.CommandFailureException: { “serverUsed” : “xxxxx” , “errmsg” : “exception: aggregation result exceeds maximum document size (16MB)” , “code” : 16389 , “ok” : 0.0}

I use mongodb 3

please help..

Lucas
7 years ago

Just add output type as Cursor.