[Mongoose] How to fix RangeError: attempt to write outside buffer bounds
I’ll introduce how to resolve RangeError: attempt to write outside buffer bounds that occurred in a system with Express (Node.js) + Mongoose (MongoDB) configuration.
BSON Document Size The maximum BSON document size is 16 megabytes.
1 MB = 1,048,576 B
So,
var str = '';
var MB = 1048576;
for (var i = 0; i < 16 * MB; i++) {
str += '0';
}
db.test.insert({ 'a' : str })
/**
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 2,
"errmsg" : "object to insert too large. size in bytes: 16777246, max size: 16777216"
}
})
**/
var str = '';
var MB = 1048576;
var diff = 30;
for (var i = 0; i < ((16 * MB) - diff); i++) {
str += '0';
}
db.test.insert({ 'a' : str })
// WriteResult({ "nInserted" : 1 })
Object.bsonsize(db.test.find().sort({ _id : -1 })[0])
// 16777216
The size being saved to 1 document is too large, so make it smaller. I don’t think 1 document would normally become 16 MB under normal usage…
That’s all from the Gemba where I was suddenly surprised by a RangeError in Mongoose.