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
- Use only supported compressors: 'snappy', 'zstd', 'zlib' (or omit compressors for none).
- Correct typos in the compressors connection-string parameter.
- Install optional native deps: npm install snappy and/or mongodb-js/zstd for the chosen compressor.
- 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
- Use only supported compressors: snappy, zstd, zlib.
- Install optional native deps (snappy, zstd) for the chosen compressor.
- Validate compressor names at config load.
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
- Message body and message header must be the same length
- Server sent message compressed using an unsupported…
- A query document must be specified for query
- Argument "size" must be a non-negative number
- authMechanism not supported
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)