49

I get the mongo error exceeded memory limit with error code 16819 when I use aggregation sort.

Im using mongo 2.6.

The query is as follows:

db.BASE_TABLE_CREATION_ExecuteHiveScript_26_V0.aggregate([
     { "$project" : { "visitor_localdate" : 1 , "_id" : 0}}, 
     { "$sort" : { "visitor_localdate" : -1}}
])

9 Answers 9

59

By default aggregation in MongoDB occurs in memory and pipeline stages have limit of 100 Mb RAM. Looks like you have exceeded this threshold. To handle large dataset you should enable aggregation pipeline stages to write data to temporary files. Use allowDiskUse option for that:

db.BASE_TABLE_CREATION_ExecuteHiveScript_26_V0.aggregate([
    { "$project" : { "visitor_localdate" : 1 , "_id" : 0}},
    { "$sort" : { "visitor_localdate" : -1}}
], { "allowDiskUse" : true })
Sign up to request clarification or add additional context in comments.

5 Comments

Hi Sergey, I tried this. It dint worked.. I get the same Exception.
@acube make sure you really tried this. If you'll look at github sources of mongodb, then you'll find MongoDB Error Codes and 16819 code has very clear explanation comment and solution steps: Sort exceeded memory limit of bytes, but did not opt in to external sorting. Aborting operation. Pass allowDiskUse:true to opt in.
when using mongoose use this. db.BASE_TABLE.aggregate([]).allowDiskUse(true);
See Sonia's answer below. While this will work, if you can move the sort earlier in the pipeline so that MongoDb can utilize an index, the query will not require a memory sort in the first place. This will make it much more performant on large data sets.
the actual solution is Sonia's answer - stackoverflow.com/a/55491963/4516910
35

In case you are using aggregate queries.Put an index on the field by which you are sorting and then use sort operator.

Note: Place the sort operator at the beginning of the pipeline or before the $project, $unwind, and $group aggregation operators. If $project, $unwind, or $group occur prior to the $sort operation, $sort cannot use any indexes.

https://docs.mongodb.com/manual/reference/operator/aggregation/sort

5 Comments

thanks for this, another thing is that for large dataset, do stream and pause resume your stream
Thanks for the Note, I ran into this exact problem of running unwind before sort and kept getting this error. swapping them around fixed it!
This is the actual solution. The allowDiskUse is just a workaround.
thanks for this! just helped me address a sizable perf issue in my app.
22

Use { allowDiskUse: true } just after aggregation pipeline, like below:

db.collectionOrView.aggregate([], { allowDiskUse: true });

Comments

6

In my scenerio, I fixed it by adding an index for the sorted column

1 Comment

Setting index is the best solution of this problem, safe and no error prone
5

You don't need aggregation for this at all. Use the query

db.BASE_TABLE_CREATION_ExecuteHiveScript_26_V0.find({}, { "_id" : 0, "visitor_localdate" : 1 }).sort({ "visitor_localdate" : -1 })

and put an index on visitor_localdate. This is simpler and faster than aggregation.

1 Comment

I am getting this error :- "errmsg" : "Executor error during find command: OperationFailed: Sort operation used more than the maximum 33554432 bytes of RAM. Add an index, or specify a smaller limit.",
5

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

The solution

Reference: Memory Restrictions

In MongoDB, the maximum memory limit for in-sort is 100M, and if you perform a larger sort, you need to use the allowDiskUse option to write the data to a temporary file to sort.

Add the allowDiskUse option to the query:

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

Comments

3

For Mongoose

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

from https://mongoosejs.com/docs/api.html#query_Query-allowDiskUse

Comments

2

For those looking for an answer for pymongo

and obtain AttributeError: 'dict' object has no attribute '_txn_read_preference'

This works:

db.coll.aggregate([], allowDiskUse=True)

Comments

2

For me worked a combination of factors:

  1. As already have been told I used { allowDiskUse: true } for my typeorm aggregation

  2. I had to put { $sort: {} } before the aggregation.

    [{ $sort: {} }, ...aggregation]

And finally it worked!

1 Comment

Sorting definitely did it for me too but I had to sort by something. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.