Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort exceeded memory limit of 104857600 bytes, but did not opt in to external sorting. Aborting operation. Pass allowDiskUse:true to opt in

Sort exceeded memory limit of 104857600 bytes, but did not opt in to external sorting. Aborting operation.

like image 949
pranita mohite Avatar asked Feb 02 '19 06:02

pranita mohite


3 Answers

As it shows, you need to pass {allowDiskUse : true} to mongo. Just append it at the end of your query.

db.collection.aggregate([
                     ...
                    ],
                     {
                       allowDiskUse: true
                     }
                    );
like image 75
RichieK Avatar answered Sep 21 '22 07:09

RichieK


For people coming from a SQL background who are seeing this. We normally put our "ORDER BY" clauses at the tail end of a query. And I instinctively had put my "sort" clause as the last part of an aggregate.

With Mongo aggregates, I think there is less wiggle room for the optimizer to intelligently move your "sort" clause around. So if logically it works for your query, get the "sort" clauses at the top, in conjunction with an index, and you can optimize your way out of the issue.

[
   {$match: ...},
   {$project: ...},
   {sort: {time: -1}},
]

=>

[
   {sort: {time: -1}},
   {$match: ...},
   {$project: ...}
]
like image 32
mpr Avatar answered Sep 21 '22 07:09

mpr


If you are using mongoose just try this :

await Model.aggregate([{ $match: { foo: 'bar' } }]).allowDiskUse(true);

see documentation

like image 36
Bouidia Abderrahmene Avatar answered Sep 21 '22 07:09

Bouidia Abderrahmene