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

2 comments on “MongoDB : Sort exceeded memory limit of 104857600 bytes

  1. 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..

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *