{"id":"c8caa67d4130d8a1","repo":"mongodb/node-mongodb-native","slug":"client-bulk-write-operation-name-of-length-bu","errorCode":null,"errorMessage":"Client bulk write operation ${name} of length ${buffer.length} exceeds the max bson object size of ${maxBsonObjectSize}","messagePattern":"Client bulk write operation (.+?) of length (.+?) exceeds the max bson object size of (.+?)","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/operations/client_bulk_write/command_builder.ts","lineNumber":238,"sourceCode":"    }\n    // Add let if it was present in the options.\n    if (this.options.let) {\n      command.let = this.options.let;\n    }\n\n    // we check for undefined specifically here to allow falsy values\n    // eslint-disable-next-line no-restricted-syntax\n    if (this.options.comment !== undefined) {\n      command.comment = this.options.comment;\n    }\n\n    return command;\n  }\n}\n\nfunction validateBufferSize(name: string, buffer: Uint8Array, maxBsonObjectSize: number) {\n  if (buffer.length > maxBsonObjectSize) {\n    throw new MongoInvalidArgumentError(\n      `Client bulk write operation ${name} of length ${buffer.length} exceeds the max bson object size of ${maxBsonObjectSize}`\n    );\n  }\n}\n\n/** @internal */\nexport interface ClientInsertOperation {\n  insert: number;\n  document: OptionalId<Document>;\n}\n\n/**\n * Build the insert one operation.\n * @param model - The insert one model.\n * @param index - The namespace index.\n * @returns the operation.\n */\nexport const buildInsertOneOperation = (","sourceCodeStart":220,"sourceCodeEnd":256,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/operations/client_bulk_write/command_builder.ts#L220-L256","documentation":"Thrown by validateBufferSize() when a single serialized operation or nsInfo entry exceeds the server's maxBsonObjectSize (16 MiB by default). This is per-document, not per-batch: one model's filter/update/document alone is too large. MongoInvalidArgumentError fires before the command is sent.","triggerScenarios":"An insertOne document, an updateMany update document, or a filter that individually serializes to more than 16 MiB. Large embedded binary blobs, huge arrays, or oversized $search/$lookup payloads in a single model.","commonSituations":"Storing large media or base64 blobs in a single document; very large IN-style filter arrays; embedding large nested structures; migrating oversized documents via bulk write.","solutions":["Reduce the size of the offending document — store large binaries in GridFS instead of inline.","Split oversized arrays/embedded data across multiple documents or external collections.","If the server genuinely allows a larger maxBsonObjectSize, confirm the value and that you are not exceeding it; otherwise the document must shrink."],"exampleFix":"// before\nawait client.bulkWrite([{\n  namespace: 'db.files',\n  name: 'insertOne',\n  document: { data: hugeBase64String } // >16MB\n}]);\n\n// after\nconst bucket = new GridFSBucket(db);\nconst id = await bucket.uploadBytes('file', hugeBuffer);\nawait client.bulkWrite([{\n  namespace: 'db.files',\n  name: 'insertOne',\n  document: { fileId: id, name: 'file' }\n}]);","handlingStrategy":"validation","validationCode":"const MAX = 16 * 1024 * 1024;\nfunction approxDocBytes(doc) { return JSON.stringify(doc).length; }\nif (models.some(m => approxDocBytes(m.document ?? m.update ?? m.filter) > MAX)) {\n  throw new Error('Model exceeds 16MB');\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep individual documents well under 16MB","Store large binaries in GridFS","Estimate serialized size before batching","Split oversized arrays across documents"],"tags":["bulk-write","bson-size-limit","client-bulk-write","document-size"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}