{"record":{"id":"12cc9a691ec07294","repo":"mongodb/node-mongodb-native","slug":"cannot-combine-replicaset-option-with-srvmaxhosts","errorCode":null,"errorMessage":"Cannot combine replicaSet option with srvMaxHosts","messagePattern":"Cannot combine replicaSet option with srvMaxHosts","errorType":"exception","errorClass":"MongoParseError","httpStatus":null,"severity":"error","filePath":"src/connection_string.ts","lineNumber":152,"sourceCode":"  if (\n    !options.userSpecifiedAuthSource &&\n    source &&\n    options.credentials &&\n    !AUTH_MECHS_AUTH_SRC_EXTERNAL.has(options.credentials.mechanism)\n  ) {\n    options.credentials = MongoCredentials.merge(options.credentials, { source });\n  }\n\n  if (!options.userSpecifiedReplicaSet && replicaSet) {\n    options.replicaSet = replicaSet;\n  }\n\n  if (loadBalanced === 'true') {\n    options.loadBalanced = true;\n  }\n\n  if (options.replicaSet && options.srvMaxHosts > 0) {\n    throw new MongoParseError('Cannot combine replicaSet option with srvMaxHosts');\n  }\n\n  validateLoadBalancedOptions(hostAddresses, options, true);\n\n  return hostAddresses;\n}\n\n/**\n * Checks if TLS options are valid\n *\n * @param allOptions - All options provided by user or included in default options map\n * @throws MongoAPIError if TLS options are invalid\n */\nfunction checkTLSOptions(allOptions: CaseInsensitiveMap): void {\n  if (!allOptions) return;\n  const check = (a: string, b: string) => {\n    if (allOptions.has(a) && allOptions.has(b)) {\n      throw new MongoAPIError(`The '${a}' option cannot be used with the '${b}' option`);","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/connection_string.ts#L134-L170","documentation":"srvMaxHosts caps the number of hosts the driver selects from the SRV result, while replicaSet directs the driver to discover members via the replica set itself. The two options contradict each other (manual host limit vs. topology-driven discovery), so the SRV spec forbids combining them. This check runs after TXT options are merged into options.","triggerScenarios":"A mongodb+srv:// URI or client config where both replicaSet is set (URI, TXT, or options) AND srvMaxHosts is greater than zero.","commonSituations":"Copy-pasting a replicaSet query param while adding srvMaxHosts to limit hosts in a sharded/load-balanced setup, or inheriting replicaSet from a TXT record and then setting srvMaxHosts in code.","solutions":["Remove the replicaSet option if you want srvMaxHosts to limit SRV hosts.","Or set srvMaxHosts to 0 (or omit it) if you need replicaSet-based discovery."],"exampleFix":"// before\nnew MongoClient('mongodb+srv://host/db?replicaSet=rs0&srvMaxHosts=3');\n// after\nnew MongoClient('mongodb+srv://host/db?srvMaxHosts=3');","handlingStrategy":"validation","validationCode":"function assertNoReplicaSetWithSrvMaxHosts(opts: Record<string, unknown>) {\n  if (opts.replicaSet != null && Number(opts.srvMaxHosts) > 0) {\n    throw new Error('Cannot combine replicaSet with srvMaxHosts > 0');\n  }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep SRV-related options together in one config block so the conflict is visible.","Decide upfront whether you want topology-driven discovery (replicaSet) or capped host selection (srvMaxHosts)."],"tags":["connection-string","srv","replica-set","validation"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}