Executor error during find command

Hi,

Got the following error while executing the below command.

MongoDB Enterprise > db.companies.find({}, {number_of_employees:1}).sort({number_of_employees: -1})
Error: error: {
“ok” : 0,
“errmsg” : “Executor error during find command :: caused by :: Sort operation used more than the maximum 33554432 bytes of RAM. Add an index, or specify a smaller limit.”,
“code” : 96,
“codeName” : “OperationFailed”
}

@Rafeeq_01820

What is your question? The error message is pretty explicit that your sort is too large for the amount of RAM. You need to qualify your find() query to return less documents.

Question is why it error’ed out in this case as it has only around 18801 documents,where as before this, I have sorted more than 25000 documents.

Thanks

@Rafeeq_01820

It gave an error because it ran out of memory. In general, the total amount of memory required is a complex factor of number of documents, document size, and available memory. So your guess that this would sort correctly in memory was wrong.

Thanks. So the key thing here is to identify the total amount of memory required which is a complex factor of number of documents, document size, and available memory, to have a good performance. I am taking the MongoDB performance course as well so will see if that helps to cover this.

@Rafeeq_01820

No. The key thing – as you will learn in Week 4 of M201 – is never to allow an in-memory sort if possible. :wink:

So it would depend upon how many documents are we returning? What is the average size of our documents? and if we multiply those two numbers together and it is bigger than the RAM size then we can predict that the sort is going to error out or cancel

You are right. Wherever possible avoid allowing in-memory sort. In MongoDB, sort operations can obtain the sort order by retrieving documents based on the ordering in an index (ascending or decending) . If the query planner cannot obtain the sort order from an index, it will sort the results in memory.

But Sort operations that use an index often have better performance than those that do not use an index. Sort operations that do not use index will abort when they use the max 32 MB of memory