title: How to set Buffer offset range in MongoDB, Its not allowing to upload more than 16MB file in BSON Object?
body: <p><strong>My Flow and Code:</strong></p>
  <p>Uploading image from Form Data and storing image's BSON string to Database, below are the steps and code:</p>
  <ol>
  <li>Uploading file from multer upload</li>
  <li>Reading file from filesystem - fs</li>
  <li>Converting image content string to base64 string</li>
  <li>Converting base64 string to BSON String</li>
  </ol>
  <p><strong>Configurations:</strong></p>
  <pre><code>node -v: v12.13.1
  npm -v: 6.12.1
  fs-extra: ^8.1.0
  multer: ^1.4.2
  </code></pre>
  <p><strong>Code:</strong></p>
  <pre><code>var upload = multer({ 
      storage: multer.diskStorage({
          destination: function (req, file, cb) {
              cb(null, 'uploads')
          },
          filename: function (req, file, cb) {
              cb(null, file.fieldname + '-' + Date.now())
          }
      })
  });
  upload.single('picture'), (req, res) =&gt; {
      
      let imageString = fs.readFileSync(req.file.path);
      let encodeImage = imageString.toString('base64');
      let bufferImage = Buffer.from(encodeImage, 'base64');
      var finalObj = {
          contentType: req.file.mimetype,
          image: bufferImage
      };
      db.collection('filesUpload').insertOne(finalObj, (err, result) =&gt; {
          if (err) {
              console.log(err);
          }else{
              console.log('success');
          }
      });
  
  });
  </code></pre>
  <p><strong>What is working?</strong>
  I can able to upload below 16MB images successful and can read and retrieve it properly from Database.</p>
  <h1>What is not Working?</h1>
  <p>I can not able to upload above 16MB images.</p>
  <h1>Error:</h1>
  <pre><code>RangeError [ERR_OUT_OF_RANGE]: The value of &quot;offset&quot; is out of range. It must be &gt;= 0 &amp;&amp; &lt;= 17825792. Received 18646861
  at Buffer.write (buffer.js:1019:5)
  at serializeObjectId (D:\api\node_modules\mongodb\node_modules\bson\lib\bson\parser\serializer.js:274:14)    at serializeInto (D:\api\node_modules\mongodb\node_modules\bson\lib\bson\parser\serializer.js:935:17)    
  at serializeObject (D:\api\node_modules\mongodb\node_modules\bson\lib\bson\parser\serializer.js:347:18)  
  at serializeInto (D:\api\node_modules\mongodb\node_modules\bson\lib\bson\parser\serializer.js:727:17)    
  at serializeObject (D:\api\node_modules\mongodb\node_modules\bson\lib\bson\parser\serializer.js:347:18)  
  at serializeInto (D:\api\node_modules\mongodb\node_modules\bson\lib\bson\parser\serializer.js:941:17)    
  at BSON.serialize (D:\api\node_modules\mongodb\node_modules\bson\lib\bson\bson.js:64:28)
  at Msg.serializeBson (D:\api\node_modules\mongodb\lib\core\connection\msg.js:126:22)
  at Msg.makeDocumentSegment (D:\api\node_modules\mongodb\lib\core\connection\msg.js:118:33)
  at Msg.toBin (D:\api\node_modules\mongodb\lib\core\connection\msg.js:104:25)
  at serializeCommand (D:\api\node_modules\mongodb\lib\core\connection\pool.js:779:41)
  at Pool.write (D:\api\node_modules\mongodb\lib\core\connection\pool.js:927:3)
  at _command (D:\api\node_modules\mongodb\lib\core\wireprotocol\command.js:128:10)
  at command (D:\api\node_modules\mongodb\lib\core\wireprotocol\command.js:28:5)
  at writeCommand (D:\api\node_modules\mongodb\lib\core\wireprotocol\write_command.js:47:3) {
      code: 'ERR_OUT_OF_RANGE'
  }
  </code></pre>
  <p>I am not getting how to solve this, i think its MongoDB error, thanks for your help.</p>