{"id":"dfffd1fed0c25844","repo":"mongodb/node-mongodb-native","slug":"no-client-bulk-write-models-were-provided","errorCode":null,"errorMessage":"No client bulk write models were provided.","messagePattern":"No client bulk write models were provided\\.","errorType":"exception","errorClass":"MongoClientBulkWriteExecutionError","httpStatus":null,"severity":"error","filePath":"src/operations/client_bulk_write/executor.ts","lineNumber":45,"sourceCode":" */\nexport class ClientBulkWriteExecutor {\n  private readonly client: MongoClient;\n  private readonly options: ClientBulkWriteOptions;\n  private readonly operations: ReadonlyArray<AnyClientBulkWriteModel<Document>>;\n\n  /**\n   * Instantiate the executor.\n   * @param client - The mongo client.\n   * @param operations - The user supplied bulk write models.\n   * @param options - The bulk write options.\n   */\n  constructor(\n    client: MongoClient,\n    operations: ReadonlyArray<AnyClientBulkWriteModel<Document>>,\n    options?: ClientBulkWriteOptions\n  ) {\n    if (operations.length === 0) {\n      throw new MongoClientBulkWriteExecutionError('No client bulk write models were provided.');\n    }\n\n    this.client = client;\n    this.operations = operations;\n    this.options = {\n      ordered: true,\n      bypassDocumentValidation: false,\n      verboseResults: false,\n      ...options\n    };\n\n    // If no write concern was provided, we inherit one from the client.\n    if (!this.options.writeConcern) {\n      this.options.writeConcern = WriteConcern.fromOptions(this.client.s.options);\n    }\n\n    if (this.options.writeConcern?.w === 0) {\n      if (this.options.verboseResults) {","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/operations/client_bulk_write/executor.ts#L27-L63","documentation":"Thrown by the ClientBulkWriteExecutor constructor when the models array is empty. There is nothing to send, so the executor refuses to construct an empty command. Classified as MongoClientBulkWriteExecutionError (a distinct error type for client bulk write execution failures).","triggerScenarios":"Calling client.bulkWrite([]) directly; passing an array that was filtered down to zero elements; building models from an empty input list.","commonSituations":"Conditional/filtered write pipelines that produce no models; batch jobs where all input records were skipped; off-by-one slice producing [].","solutions":["Guard the call: if (models.length > 0) await client.bulkWrite(models); else return empty result.","Validate upstream so empty batches short-circuit before reaching bulkWrite.","If models come from a map/filter, default to a no-op when the result is empty."],"exampleFix":"// before\nawait client.bulkWrite(models); // throws if []\n\n// after\nif (models.length === 0) {\n  return { insertedCount: 0, updatedCount: 0, deletedCount: 0 };\n}\nawait client.bulkWrite(models);","handlingStrategy":"validation","validationCode":"if (!Array.isArray(models) || models.length === 0) {\n  return { insertedCount: 0, updatedCount: 0, deletedCount: 0 };\n}","typeGuard":"function hasModels(arr): boolean { return Array.isArray(arr) && arr.length > 0; }","tryCatchPattern":null,"preventionTips":["Short-circuit empty batches before calling bulkWrite","Filter/skip empty arrays in pipelines","Return a synthetic empty result for no-op batches"],"tags":["bulk-write","validation","empty-input","client-bulk-write"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}