mongodb/node-mongodb-native · error · MongoInvalidArgumentError

${c} is not a valid compression mechanism. Must be one of: $

Error message

${c} is not a valid compression mechanism. Must be one of: ${Object.keys(Compressor)}.

What it means

Each entry in `compressors` must match a key of the `Compressor` enum: `none`, `snappy`, `zlib`, or `zstd`. An unrecognized name throws a MongoInvalidArgumentError at src/connection_string.ts:793.

Source

Thrown at src/connection_string.ts:793

    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];
    }
  },
  connectTimeoutMS: {
    default: 30000,
    type: 'uint'
  },
  dbName: {
    type: 'string'
  },
  directConnection: {

View on GitHub (pinned to 3366c21a63)

Solutions

  1. Use only 'none', 'snappy', 'zlib', or 'zstd'
  2. Replace 'gzip' with 'zlib'
  3. Install the optional native package for the chosen compressor (e.g. mongodb-js/zstd for zstd)

Example fix

// before
new MongoClient(uri, { compressors: ['gzip'] });
// after
new MongoClient(uri, { compressors: ['zlib'] });
Defensive patterns

Strategy: validation

Validate before calling

import { Compressor } from 'mongodb';
const VALID = new Set(Object.keys(Compressor));
function normalizeCompressors(v) {
  return typeof v === 'string' ? v.split(',') : v;
}
function allValid(v) {
  return normalizeCompressors(v).every(c => VALID.has(String(c)));
}
if (options.compressors && !allValid(options.compressors)) {
  throw new TypeError('compressors contains an invalid mechanism');
}

Type guard

import { Compressor } from 'mongodb';
function isValidCompressorName(c) {
  return Object.keys(Compressor).includes(String(c));
}

Prevention

When it happens

Trigger: `{ compressors: ['gzip'] }` (gzip is invalid — MongoDB uses 'zlib'); `{ compressors: 'brotli' }`; `{ compressors: ['lz4'] }`; `{ compressors: 'snappy,zstd,gzip' }`.

Common situations: Assuming MongoDB uses 'gzip' (it uses 'zlib'); listing a compressor whose optional native dependency isn't installed; copy-paste from HTTP compression configs.

Related errors


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