mongodb/node-mongodb-native · error · MongoInvalidArgumentError

Unknown compressor failed to compress

Error message

Unknown compressor ${options.agreedCompressor} failed to compress

What it means

A MongoInvalidArgumentError thrown in the compress() function's default switch case when options.agreedCompressor is not one of 'snappy', 'zstd', 'zlib'. The compressor is negotiated during handshake from the compressors connection-string option; reaching the default case means an unrecognized compressor value was agreed upon, which indicates corruption in the negotiated state or an invalid input to the compression layer.

Solutions

  1. Use only supported compressors: 'snappy', 'zstd', 'zlib' (or omit compressors for none).
  2. Correct typos in the compressors connection-string parameter.
  3. Install optional native deps: npm install snappy and/or mongodb-js/zstd for the chosen compressor.
  4. Upgrade the driver if using a version with a compression-negotiation bug.

Example fix

// before
const client = new MongoClient('mongodb://host/?compressors=gzip');

// after
const client = new MongoClient('mongodb://host/?compressors=zlib');
Defensive patterns

Strategy: validation

Validate before calling

const VALID = ['none','snappy','zstd','zlib'];
for (const c of compressors) {
  if (!VALID.includes(c)) throw new Error(`Unsupported compressor: ${c}`);
}

Prevention

When it happens

Trigger: The connection's agreedCompressor field holds a value outside the known set. For end users this arises when the compressors connection-string option contains a typo or unsupported name (e.g., 'gzip', 'lz4') that somehow propagated, or via internal corruption of the description object.

Common situations: Typo in ?compressors= (e.g., 'gzip' instead of 'zlib', 'snapp' instead of 'snappy'); passing an unsupported compressor name; a custom/forked driver adding a compressor without updating the switch; version skew where a compressor was renamed.

Related errors


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

Appendix: source

Thrown at src/cmap/wire_protocol/compression.ts:102

    case 'snappy': {
      Snappy ??= loadSnappy();
      return await Snappy.compress(dataToBeCompressed);
    }
    case 'zstd': {
      loadZstd();
      if ('kModuleError' in zstd) {
        throw zstd['kModuleError'];
      }
      return await zstd.compress(dataToBeCompressed, ZSTD_COMPRESSION_LEVEL);
    }
    case 'zlib': {
      if (options.zlibCompressionLevel) {
        zlibOptions.level = options.zlibCompressionLevel;
      }
      return await zlibDeflate(dataToBeCompressed, zlibOptions);
    }
    default: {
      throw new MongoInvalidArgumentError(
        `Unknown compressor ${options.agreedCompressor} failed to compress`
      );
    }
  }
}

// Decompress a message using the given compressor
export async function decompress(
  compressorID: number,
  compressedData: Uint8Array
): Promise<Uint8Array> {
  if (
    compressorID !== Compressor.snappy &&
    compressorID !== Compressor.zstd &&
    compressorID !== Compressor.zlib &&
    compressorID !== Compressor.none
  ) {
    throw new MongoDecompressionError(

View on GitHub (pinned to dce7939f86)