mongodb/node-mongodb-native · error · MongoInvalidArgumentError

compressors must be an array or a comma-delimited list of st

Error message

compressors must be an array or a comma-delimited list of strings

What it means

The `compressors` option accepts an array of compressor names or a single comma-delimited string. The transform (src/connection_string.ts:777-802) splits strings on commas; if a value is neither a string nor an array, it throws a MongoInvalidArgumentError.

Source

Thrown at src/connection_string.ts:785

            ServerApiVersion
          ).join('", "')}"]`
        );
      }
      return serverApiToValidate;
    }
  },
  checkKeys: {
    type: 'boolean'
  },
  compressors: {
    default: 'none',
    target: 'compressors',
    transform({ values }) {
      const compressionList = new Set();
      for (const compVal of values as (CompressorName[] | string)[]) {
        const compValArray = typeof compVal === 'string' ? compVal.split(',') : compVal;
        if (!Array.isArray(compValArray)) {
          throw new MongoInvalidArgumentError(
            'compressors must be an array or a comma-delimited list of strings'
          );
        }
        for (const c of compValArray) {
          if (Object.keys(Compressor).includes(String(c))) {
            compressionList.add(String(c));
          } else {
            throw new MongoInvalidArgumentError(
              `${c} is not a valid compression mechanism. Must be one of: ${Object.keys(
                Compressor
              )}.`
            );
          }
        }
      }
      return [...compressionList];
    }
  },

View on GitHub (pinned to 3366c21a63)

Solutions

  1. Pass compressors as an array: ['zstd', 'snappy']
  2. Or a comma-delimited string: 'zstd,snappy'
  3. Omit the option to use the default 'none'

Example fix

// before
new MongoClient(uri, { compressors: 1 });
// after
new MongoClient(uri, { compressors: ['zstd', 'snappy'] });
Defensive patterns

Strategy: validation

Validate before calling

function isCompressorsInput(v) {
  return typeof v === 'string' || Array.isArray(v);
}
if (options.compressors != null && !isCompressorsInput(options.compressors)) {
  throw new TypeError('compressors must be an array or comma-delimited string');
}

Type guard

function isCompressorsInput(v) {
  return typeof v === 'string' || Array.isArray(v);
}

Prevention

When it happens

Trigger: `{ compressors: 1 }`; `{ compressors: true }`; `{ compressors: { name: 'zstd' } }`; `{ compressors: null }`.

Common situations: Passing a single compressor as a non-string scalar; config-merge bugs producing the wrong type; JSON typed as number/boolean.

Related errors


AI-assisted analysis of mongodb/node-mongodb-native@3366c21a63 (2026-08-04). Data as JSON: /data/errors/d531a127138abcd6.json. Report an issue: GitHub.