{"record":{"id":"1b398d0afa50f732","repo":"mongodb/node-mongodb-native","slug":"operation-passed-in-cannot-be-an-array","errorCode":null,"errorMessage":"Operation passed in cannot be an Array","messagePattern":"Operation passed in cannot be an Array","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/bulk/ordered.ts","lineNumber":88,"sourceCode":"\n      // Create a new batch\n      this.s.currentBatch = new Batch(batchType, this.s.currentIndex);\n\n      // Reset the current size trackers\n      this.s.currentBatchSize = 0;\n      this.s.currentBatchSizeBytes = 0;\n    }\n\n    if (batchType === BatchType.INSERT) {\n      this.s.bulkResult.insertedIds.push({\n        index: this.s.currentIndex,\n        _id: (document as Document)._id\n      });\n    }\n\n    // We have an array of documents\n    if (Array.isArray(document)) {\n      throw new MongoInvalidArgumentError('Operation passed in cannot be an Array');\n    }\n\n    this.s.currentBatch.originalIndexes.push(this.s.currentIndex);\n    this.s.currentBatch.operations.push(document);\n    if (buffer != null) this.s.currentBatch.serializedOperations.push(buffer);\n    this.s.currentBatchSize += 1;\n    this.s.currentBatchSizeBytes += maxKeySize + bsonSize;\n    this.s.currentIndex += 1;\n    return this;\n  }\n}\n","sourceCodeStart":70,"sourceCodeEnd":100,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/bulk/ordered.ts#L70-L100","documentation":"Thrown as MongoInvalidArgumentError by OrderedBulkOperation.addToOperationsList() (src/bulk/ordered.ts:88) when the document passed to addToOperationsList is a JavaScript Array. addToOperationsList expects a single BSON document (or update/delete statement); passing an array is a structural mistake because the bulk op processes one op at a time and has already serialized the value as if it were a document.","triggerScenarios":"An internal/builder caller (or a user reaching addToOperationsList via raw() with a malformed insert) passes an Array where a Document is expected. Guarded by `if (Array.isArray(document))` at src/bulk/ordered.ts:87.","commonSituations":"Mostly an internal-driver or advanced-usage error; can surface if a user constructs an operation whose `document`/`update`/`replacement` field is an array where the API expects a single object, or when wrapping the driver in a custom layer that forwards arrays. Very rare through the standard bulkWrite() path.","solutions":["Ensure the value passed for a single op is a plain object, not an array — iterate and add each element as its own operation if you have many.","If you have an array of documents to insert, call bulk.insert(doc) per element rather than bulk.insert(docArray).","Audit any custom wrapper that forwards payloads into addToOperationsList to confirm it passes documents one at a time."],"exampleFix":"// before: passing an array where a single document is expected\nfor (const doc of docs) {\n  // some custom path ends up calling addToOperationsList(BatchType.INSERT, docs)\n}\n\n// after: add each document individually\nfor (const doc of docs) {\n  bulk.insert(doc); // one document per call\n}","handlingStrategy":"type-guard","validationCode":"function isSingleDocument(doc: unknown): boolean {\n  return doc != null && typeof doc === 'object' && !Array.isArray(doc);\n}\n\nfor (const op of ops) {\n  const doc = (op as any).insertOne?.document;\n  if (doc != null && !isSingleDocument(doc)) {\n    throw new Error('expected a single document, got an array');\n  }\n}","typeGuard":"function isSingleDocument(doc: unknown): doc is Record<string, unknown> {\n  return doc != null && typeof doc === 'object' && !Array.isArray(doc);\n}","tryCatchPattern":"try {\n  await bulk.execute();\n} catch (e) {\n  if (e instanceof MongoInvalidArgumentError && /cannot be an Array/.test(e.message)) {\n    // flatten any array-valued document into one insert per element, then re-execute\n  } else throw e;\n}","preventionTips":["Pass one document per insert/update/delete call; iterate arrays externally.","Type document parameters as Record<string, unknown>, not as unknown[] or any[].","Validate at API boundaries that payloads are not arrays.","Audit custom driver wrappers to ensure they forward single documents."],"tags":["bulk-write","ordered","type-mismatch","validation"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}