{"record":{"id":"982206892efff534","repo":"mongodb/node-mongodb-native","slug":"argument-operations-must-be-an-array-of-document","errorCode":null,"errorMessage":"Argument \"operations\" must be an array of documents","messagePattern":"Argument \"operations\" must be an array of documents","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/collection.ts","lineNumber":367,"sourceCode":"   * - `updateOne`\n   * - `updateMany`\n   * - `deleteOne`\n   * - `deleteMany`\n   *\n   * If documents passed in do not contain the **_id** field,\n   * one will be added to each of the documents missing it by the driver, mutating the document. This behavior\n   * can be overridden by setting the **forceServerObjectId** flag.\n   *\n   * @param operations - Bulk operations to perform\n   * @param options - Optional settings for the command\n   * @throws MongoDriverError if operations is not an array\n   */\n  async bulkWrite(\n    operations: ReadonlyArray<AnyBulkWriteOperation<TSchema>>,\n    options?: BulkWriteOptions\n  ): Promise<BulkWriteResult> {\n    if (!Array.isArray(operations)) {\n      throw new MongoInvalidArgumentError('Argument \"operations\" must be an array of documents');\n    }\n\n    options = resolveOptions(this, options ?? {});\n\n    // TODO(NODE-7071): remove once the client doesn't need to be connected to construct\n    // bulk operations\n    const isConnected = this.client.topology != null;\n    if (!isConnected) {\n      await autoConnect(this.client);\n    }\n\n    // Create the bulk operation\n    const bulk: BulkOperationBase =\n      options.ordered === false\n        ? this.initializeUnorderedBulkOp(options)\n        : this.initializeOrderedBulkOp(options);\n\n    // for each op go through and add to the bulk","sourceCodeStart":349,"sourceCodeEnd":385,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/collection.ts#L349-L385","documentation":"Thrown by Collection.bulkWrite when its first argument is not an array. The method requires ReadonlyArray<AnyBulkWriteOperation<TSchema>>; a non-array is a programming error and raises a MongoInvalidArgumentError before constructing the bulk operation.","triggerScenarios":"Fires at src/collection.ts:367 when `!Array.isArray(operations)`. Occurs when a caller passes a single operation object, undefined, null, or a non-array iterable to bulkWrite.","commonSituations":"Passing one operation without wrapping in an array; passing undefined from an optional param; feeding a generator/Set; refactoring that lost the array literal; dynamic building that never wrapped results.","solutions":["Wrap operations in an array: bulkWrite([{ insertOne: {...} }]).","If you have a single operation, still pass it as a one-element array.","Guard dynamic inputs with Array.isArray before calling.","Type the operations variable explicitly so TS flags misuse."],"exampleFix":"// before\nawait collection.bulkWrite({ insertOne: { document: { a: 1 } } });\n\n// after\nawait collection.bulkWrite([{ insertOne: { document: { a: 1 } } }]);","handlingStrategy":"validation","validationCode":"if (!Array.isArray(operations)) {\n  throw new TypeError('operations must be an array');\n}\nawait collection.bulkWrite(operations);","typeGuard":"function isBulkOpArray(v: unknown): v is Record<string, unknown>[] {\n  return Array.isArray(v) && v.every(o => o != null && typeof o === 'object');\n}","tryCatchPattern":null,"preventionTips":["Always wrap bulk operations in an array literal.","Guard dynamically-built operation lists with Array.isArray.","Type operations as AnyBulkWriteOperation[]."],"tags":["validation","bulk-write","typescript","api-misuse"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}