{"record":{"id":"40a693d1128c836c","repo":"mongodb/node-mongodb-native","slug":"operation-passed-in-cannot-be-an-array-40a693","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/unordered.ts","lineNumber":103,"sourceCode":"      // New batch if we exceed the max batch op size\n      this.s.currentBatch.size + 1 >= this.s.maxWriteBatchSize ||\n      // New batch if we exceed the maxBatchSizeBytes. Only matters if batch already has a doc,\n      // since we can't sent an empty batch\n      (this.s.currentBatch.size > 0 &&\n        this.s.currentBatch.sizeBytes + maxKeySize + bsonSize >= this.s.maxBatchSizeBytes) ||\n      // New batch if the new op does not have the same op type as the current batch\n      this.s.currentBatch.batchType !== batchType\n    ) {\n      // Save the batch to the execution stack\n      this.s.batches.push(this.s.currentBatch);\n\n      // Create a new batch\n      this.s.currentBatch = new Batch(batchType, this.s.currentIndex);\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.operations.push(document);\n    if (buffer != null) this.s.currentBatch.serializedOperations.push(buffer);\n    this.s.currentBatch.originalIndexes.push(this.s.currentIndex);\n    this.s.currentIndex = this.s.currentIndex + 1;\n\n    // Save back the current Batch to the right type\n    if (batchType === BatchType.INSERT) {\n      this.s.currentInsertBatch = this.s.currentBatch;\n      this.s.bulkResult.insertedIds.push({\n        index: this.s.bulkResult.insertedIds.length,\n        _id: (document as Document)._id\n      });\n    } else if (batchType === BatchType.UPDATE) {\n      this.s.currentUpdateBatch = this.s.currentBatch;\n    } else if (batchType === BatchType.DELETE) {\n      this.s.currentRemoveBatch = this.s.currentBatch;","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/bulk/unordered.ts#L85-L121","documentation":"Thrown by UnorderedBulkOperation.addToOperationsList() when the operation document passed to a bulk write method is a JavaScript Array instead of a plain object. Each bulk operation (insert, update, delete) must be a single BSON document; an array is not a valid operation shape. This is a MongoInvalidArgumentError.","triggerScenarios":"Calling bulk.insert([...]) with an array instead of a single document object, or manually calling addToOperationsList() with an array-typed document. More commonly reached when a caller iterates over a list and forgets to spread or destructure, passing the array container rather than an element.","commonSituations":"Looping over an array of documents and accidentally passing the outer array instead of an individual element; migrating from insertMany (which takes an array) to the bulk API (which takes individual documents); JavaScript code that bypasses TypeScript type safety at runtime.","solutions":["Ensure each call to bulk.insert() receives a single document object, not an array","If you have an array of documents, loop and call insert for each: docs.forEach(d => bulk.insert(d))","Use collection.insertMany(docs) instead of bulk operations if you have a pre-built array of insert documents","Add a runtime type check (Array.isArray(doc)) before calling bulk methods to catch this in JavaScript code"],"exampleFix":"// before\nconst docs = [{ a: 1 }, { a: 2 }];\nbulk.insert(docs); // passes the array itself\n\n// after\nfor (const doc of docs) {\n  bulk.insert(doc); // passes each document individually\n}","handlingStrategy":"validation","validationCode":"// Before calling bulk.insert(doc)\nif (Array.isArray(doc)) {\n  throw new TypeError('Bulk operation document must not be an array; pass individual documents');\n}\nbulk.insert(doc);","typeGuard":"function isBulkOperationDoc(doc: unknown): doc is Record<string, unknown> {\n  return typeof doc === 'object' && doc !== null && !Array.isArray(doc);\n}","tryCatchPattern":"try {\n  bulk.insert(doc);\n} catch (error) {\n  if (error instanceof MongoInvalidArgumentError && error.message.includes('cannot be an Array')) {\n    // Handle array misuse: iterate and insert individually\n  }\n  throw error;\n}","preventionTips":["Use TypeScript type annotations to enforce Document types at compile time","When migrating from insertMany to bulk ops, remember bulk takes individual documents not arrays","Run a quick Array.isArray() check on dynamic data before bulk operations"],"tags":["bulk-write","validation","typescript"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}