{"record":{"id":"d531a127138abcd6","repo":"mongodb/node-mongodb-native","slug":"compressors-must-be-an-array-or-a-comma-delimited","errorCode":null,"errorMessage":"compressors must be an array or a comma-delimited list of strings","messagePattern":"compressors must be an array or a comma-delimited list of strings","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/connection_string.ts","lineNumber":785,"sourceCode":"            ServerApiVersion\n          ).join('\", \"')}\"]`\n        );\n      }\n      return serverApiToValidate;\n    }\n  },\n  checkKeys: {\n    type: 'boolean'\n  },\n  compressors: {\n    default: 'none',\n    target: 'compressors',\n    transform({ values }) {\n      const compressionList = new Set();\n      for (const compVal of values as (CompressorName[] | string)[]) {\n        const compValArray = typeof compVal === 'string' ? compVal.split(',') : compVal;\n        if (!Array.isArray(compValArray)) {\n          throw new MongoInvalidArgumentError(\n            'compressors must be an array or a comma-delimited list of strings'\n          );\n        }\n        for (const c of compValArray) {\n          if (Object.keys(Compressor).includes(String(c))) {\n            compressionList.add(String(c));\n          } else {\n            throw new MongoInvalidArgumentError(\n              `${c} is not a valid compression mechanism. Must be one of: ${Object.keys(\n                Compressor\n              )}.`\n            );\n          }\n        }\n      }\n      return [...compressionList];\n    }\n  },","sourceCodeStart":767,"sourceCodeEnd":803,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/connection_string.ts#L767-L803","documentation":"The `compressors` connection option accepts either an array of compressor names (e.g. ['zlib','snappy']) or a single comma-delimited string (e.g. 'zlib,snappy'). The transform at src/connection_string.ts:780 first splits a string on commas, otherwise uses the value as-is; if that result is not an Array, the driver throws MongoInvalidArgumentError at src/connection_string.ts:785. This guard rejects values whose type the option can never consume (numbers, booleans, plain objects).","triggerScenarios":"Calling `new MongoClient(uri, { compressors: 1 })`, `new MongoClient(uri, { compressors: true })`, `new MongoClient(uri, { compressors: { zlib: true } })`, or `new MongoClient('mongodb://host?compressors=...')` with a value that is neither an array nor a string. The error is thrown during MongoClient construction while the options table transforms are applied.","commonSituations":"Loading `compressors` from an environment variable or JSON config that decodes to a non-string/non-array type (e.g. a number flag, a feature-toggle object); downstream tooling injecting the wrong type; copy-pasting a comma string but accidentally wrapping it as another type.","solutions":["Pass an array: `new MongoClient(uri, { compressors: ['zlib'] })`.","Or pass a comma-delimited string: `new MongoClient(uri, { compressors: 'zlib,snappy' })`.","Inspect the source of the value (env var / config loader) and coerce it to a string or array before passing it to MongoClient."],"exampleFix":"// before\nnew MongoClient(uri, { compressors: 1 });\n\n// after\nnew MongoClient(uri, { compressors: ['zlib'] });","handlingStrategy":"type-guard","validationCode":"function isValidCompressorsInput(v: unknown): boolean {\n  return typeof v === 'string' || Array.isArray(v);\n}\nif (!isValidCompressorsInput(opts.compressors)) {\n  throw new Error('compressors must be a string or array');\n}","typeGuard":"function isCompressorsInput(v: unknown): v is string | unknown[] {\n  return typeof v === 'string' || Array.isArray(v);\n}","tryCatchPattern":"try { client = new MongoClient(uri, opts); } catch (e) { if (e instanceof MongoInvalidArgumentError && /compressors/.test(e.message)) { /* fix opts */ } else throw e; }","preventionTips":["Type the options object as MongoClientOptions so the compiler rejects wrong types.","When reading from env/config, coerce the value to a string before passing it in.","Add a unit test that constructs MongoClient with your production config object."],"tags":["connection-options","compression","configuration","type-error"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}