39

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}}
])
Devkinandan Chauhan
  • 1,785
  • 1
  • 17
  • 42
acube
  • 491
  • 1
  • 4
  • 3

9 Answers9

53

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 })
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • Hi Sergey, I tried this. It dint worked.. I get the same Exception. – acube Oct 16 '14 at 10:21
  • @acube make sure you really tried this. If you'll look at github sources of mongodb, then you'll find [MongoDB Error Codes](https://github.com/mongodb/mongo/blob/master/docs/errors.md) 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.* – Sergey Berezovskiy Oct 16 '14 at 12:05
  • 6
    when using mongoose use this. `db.BASE_TABLE.aggregate([]).allowDiskUse(true);` – kheengz Jan 22 '21 at 13:02
  • 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. – Kip Jul 26 '21 at 21:11
  • the actual solution is Sonia's answer - https://stackoverflow.com/a/55491963/4516910 – Avraham Shalev Jun 11 '23 at 12:36
27

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

Niki-Timofe
  • 105
  • 1
  • 6
sonia kaushal
  • 495
  • 6
  • 5
22

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

db.collectionOrView.aggregate([], { allowDiskUse: true });
4b0
  • 21,981
  • 30
  • 95
  • 142
Devkinandan Chauhan
  • 1,785
  • 1
  • 17
  • 42
4

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.

wdberkeley
  • 11,531
  • 1
  • 28
  • 23
  • 2
    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.", – ak3191 Oct 12 '18 at 19:46
4

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}
);
Nader Gharibian Fard
  • 6,417
  • 4
  • 12
  • 22
4

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

Charlie
  • 2,141
  • 3
  • 19
  • 35
3

For Mongoose

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

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

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!

DragoRoff
  • 55
  • 4
1

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)