{"id":"2cfc8e5bcd8da5ac","repo":"mongodb/node-mongodb-native","slug":"document-is-larger-than-the-maximum-size-this-s-2cfc8e","errorCode":null,"errorMessage":"Document is larger than the maximum size ${this.s.maxBsonObjectSize}","messagePattern":"Document is larger than the maximum size (.+?)","errorType":"validation","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/bulk/unordered.ts","lineNumber":60,"sourceCode":"    if (this.s.usingAutoEncryption) {\n      bsonSize = BSON.calculateObjectSize(document, {\n        checkKeys: false,\n        ignoreUndefined: false\n      } as any);\n    } else {\n      const bson = this.s.bsonOptions;\n      buffer = BSON.serialize(document, {\n        checkKeys: this.s.checkKeys,\n        ignoreUndefined: bson.ignoreUndefined,\n        serializeFunctions: bson.serializeFunctions\n      });\n      bsonSize = buffer.length;\n    }\n\n    // Throw error if the doc is bigger than the max BSON size\n    if (bsonSize >= this.s.maxBsonObjectSize) {\n      // TODO(NODE-3483): Change this to MongoBSONError\n      throw new MongoInvalidArgumentError(\n        `Document is larger than the maximum size ${this.s.maxBsonObjectSize}`\n      );\n    }\n\n    // Holds the current batch\n    this.s.currentBatch = undefined;\n    // Get the right type of batch\n    if (batchType === BatchType.INSERT) {\n      this.s.currentBatch = this.s.currentInsertBatch;\n    } else if (batchType === BatchType.UPDATE) {\n      this.s.currentBatch = this.s.currentUpdateBatch;\n    } else if (batchType === BatchType.DELETE) {\n      this.s.currentBatch = this.s.currentRemoveBatch;\n    }\n\n    const maxKeySize = this.s.maxKeySize;\n\n    // Create a new batch object if we don't have a current one","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/bulk/unordered.ts#L42-L78","documentation":"Thrown by UnorderedBulkOperation.addToOperationsList() when a single operation's serialized BSON size is >= the server's maxBsonObjectSize (typically 16MB). Identical semantics to the ordered variant; the unordered batch type just routes through a different addToOperationsList implementation.","triggerScenarios":"Inserting a very large document via unordered bulk.insert(). Updating with a multi-megabyte $set value in an unordered batch.","commonSituations":"Loading large media/log payloads inline; migrating data from a store without a size limit.","solutions":["Move large blobs to GridFS or external storage and keep references in the document.","Split or chunk the document.","Verify the server's reported maxBsonObjectSize to know the real ceiling."],"exampleFix":"// before\nbulk.insert({ data: hugeBuffer }); // > 16MB\n\n// after\nconst id = await bucket.upload(hugeBuffer);\nbulk.insert({ contentId: id, metadata: {...} });","handlingStrategy":"validation","validationCode":"import * as BSON from 'bson';\nfunction assertUnderMaxSize(doc, maxBsonObjectSize = 16 * 1024 * 1024) {\n  const size = BSON.serialize(doc).length;\n  if (size >= maxBsonObjectSize) {\n    throw new RangeError(`document serialized to ${size} bytes, exceeds ${maxBsonObjectSize}`);\n  }\n}","typeGuard":"function isWithinBsonLimit(doc, maxBsonObjectSize = 16 * 1024 * 1024) {\n  try {\n    return BSON.serialize(doc).length < maxBsonObjectSize;\n  } catch {\n    return false;\n  }\n}","tryCatchPattern":"try {\n  bulk.insert(doc);\n} catch (e) {\n  if (e instanceof MongoInvalidArgumentError && /larger than the maximum size/.test(e.message)) {\n    // route to GridFS or chunk the document\n  }\n}","preventionTips":["Keep large blobs out of inline documents; use GridFS.","Measure serialized size when handling untrusted payloads.","Read the server's reported maxBsonObjectSize to know the real ceiling."],"tags":["bulk-write","unordered","bson","size-limit"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}