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
- Pass compressors as an array: ['zstd', 'snappy']
- Or a comma-delimited string: 'zstd,snappy'
- 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
- Pass compressors as an array of strings
- Validate the type before constructing the client when config is dynamic
- Omit the option to keep the default 'none'
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
- ${c} is not a valid compression mechanism. Must be one of: $
- Unknown compressor ${options.agreedCompressor} failed to com
- Descriptors missing a type must define a transform
- ${name} must be an object with 'username' and 'password' pro
- authMechanism one of ${mechanisms}, got ${value}
AI-assisted analysis of mongodb/node-mongodb-native@3366c21a63 (2026-08-04).
Data as JSON: /data/errors/d531a127138abcd6.json.
Report an issue: GitHub.