[MongoDB] How to Fix "sort stage buffered data usage of x bytes exceeds internal limit of 33554432 bytes" Error
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.
The web application architecture when the error occurred was as follows:
I’ll introduce both the code that caused the error and the code that solved it in Node.js + Mongoose.
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 });
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
});
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.