[Mongoose] How to fix RangeError: attempt to write outside buffer bounds

Tadashi Shigeoka ·  Thu, November 22, 2018

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.

mongoose | マングース

(Verification) Maximum size of BSON document is 16 MB

(Error) object to insert too large. size in bytes: xxx, max size: 16777216

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"
  }
})
**/

(Success) 16777216 bytes can be inserted with db.collection.insert()

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

Solution for RangeError

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.

References