mongodb/node-mongodb-native · error · MongoClientBulkWriteExecutionError

No client bulk write models were provided.

Error message

No client bulk write models were provided.

What it means

Thrown by the client bulk write executor when the models array is empty. The driver does not issue an empty bulkWrite command to the server because that would be a wasted round trip; instead it fails fast with this client-side error.

Solutions

  1. Guard with if (models.length > 0) before calling client.bulkWrite(models).
  2. Log a warning when a scheduled batch is empty — it usually signals an upstream logic gap.
  3. If you expect this case often, wrap bulkWrite in a helper that short-circuits on empty input.

Example fix

// before
await client.bulkWrite(models); // models may be []

// after
if (models.length === 0) return { insertedCount: 0, modifiedCount: 0 };
await client.bulkWrite(models);
Defensive patterns

Strategy: validation

Validate before calling

if (!Array.isArray(models) || models.length === 0) {
  return { insertedCount: 0, modifiedCount: 0, deletedCount: 0 };
}
await client.bulkWrite(models);

Type guard

const hasModels = (v: unknown): v is unknown[] => Array.isArray(v) && v.length > 0;

Prevention

When it happens

Trigger: Calling client.bulkWrite([]) directly, or passing an array that has been filtered down to zero elements before reaching bulkWrite.

Common situations: Building a models array conditionally and then calling bulkWrite without checking length; debouncing/batching code that fires on a timer even when no work accumulated; refactoring that drops the population step.

Related errors


AI-assisted analysis of mongodb/node-mongodb-native@dce7939f86 (2026-08-11). Data as JSON: /api/errors/dfffd1fed0c25844. Report an issue: GitHub.

Appendix: source

Thrown at src/operations/client_bulk_write/executor.ts:45

 */
export class ClientBulkWriteExecutor {
  private readonly client: MongoClient;
  private readonly options: ClientBulkWriteOptions;
  private readonly operations: ReadonlyArray<AnyClientBulkWriteModel<Document>>;

  /**
   * Instantiate the executor.
   * @param client - The mongo client.
   * @param operations - The user supplied bulk write models.
   * @param options - The bulk write options.
   */
  constructor(
    client: MongoClient,
    operations: ReadonlyArray<AnyClientBulkWriteModel<Document>>,
    options?: ClientBulkWriteOptions
  ) {
    if (operations.length === 0) {
      throw new MongoClientBulkWriteExecutionError('No client bulk write models were provided.');
    }

    this.client = client;
    this.operations = operations;
    this.options = {
      ordered: true,
      bypassDocumentValidation: false,
      verboseResults: false,
      ...options
    };

    // If no write concern was provided, we inherit one from the client.
    if (!this.options.writeConcern) {
      this.options.writeConcern = WriteConcern.fromOptions(this.client.s.options);
    }

    if (this.options.writeConcern?.w === 0) {
      if (this.options.verboseResults) {

View on GitHub (pinned to dce7939f86)