Sort exceeded memory limit of 104857600 bytes, but did not opt in to external sorting. Aborting operation.
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
}
);
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: ...}
]
If you are using mongoose just try this :
await Model.aggregate([{ $match: { foo: 'bar' } }]).allowDiskUse(true);
see documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With