[MongoDB] How to Fix "sort stage buffered data usage of x bytes exceeds internal limit of 33554432 bytes" Error

Tadashi Shigeoka ·  Wed, October 18, 2017

In a Node.js + MongoDB web application, I suddenly started getting the error Unable to execute query: errmsg: “sort stage buffered data usage of x bytes exceeds internal limit of 33554432 bytes”.

I was able to solve it by defining an index on MongoDB fields, so I’ll share that practical example.

MongoDB | モンゴディービー

Architecture

The web application architecture when the error occurred was as follows:

  • Express.js (Node.js)
  • Mongoose (MongoDB)

Example: Error When Sorting on Field Without Index Defined in Mongoose

I’ll introduce both the code that caused the error and the code that solved it in Node.js + Mongoose.

Code That Caused the Error

The code that caused the error simply didn’t have an index defined on the field used in sort().

const userScheme = new Scheme({
  // omitted
  loginAt: {
    type: Date
  }
});

// find query
User.find().sort({ loginAt : -1 });

Code That Solved the Problem

Just properly define an index as shown below and you’re good to go.

const userScheme = new Scheme({
  // omitted
  loginAt: {
    type: Date,
    index: true
  }
});

Or alternatively:

userScheme.index({
  loginAt: 1
});

Summary

This error occurs when there are approximately 5,000 to 6,000 documents stored in one MongoDB collection.

I’d like to create a system to regularly check slow queries before they become fatal errors.

That’s all from the Gemba, where I don’t want to be caught by the slow query police.