{"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/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/connection_string.ts#L134-L170","documentation":"Thrown by resolveSRVRecord() when, for a mongodb+srv:// URI, both the replicaSet option is set and srvMaxHosts is greater than 0. srvMaxHosts limits the number of hosts selected from the SRV result, which is incompatible with replicaSet semantics (the driver must know all members). This is a MongoParseError raised during SRV resolution.","triggerScenarios":"URI like mongodb+srv://host/db?replicaSet=rs0&srvMaxHosts=3; passing srvMaxHosts via the options object while replicaSet is in the URI (or TXT record); load-balanced or sharded setups where srvMaxHosts was copied from a template that also sets replicaSet.","commonSituations":"Copying an Atlas URI template that includes replicaSet then adding srvMaxHosts to limit hosts; mixing TXT-provided replicaSet with a client option srvMaxHosts; misunderstanding that srvMaxHosts is for sharded clusters without replicaSet.","solutions":["Drop srvMaxHosts from the URI/options when connecting to a replica set.","Drop replicaSet from the URI (and TXT record) when using srvMaxHosts against a load-balanced/sharded front.","Re-check the TXT record does not inject replicaSet while you set srvMaxHosts client-side.","Validate the resolved options by logging them before connect in non-production."],"exampleFix":"// before\nmongodb+srv://cluster.example.net/db?replicaSet=rs0&srvMaxHosts=3\n// after (replica set)\nmongodb+srv://cluster.example.net/db?replicaSet=rs0\n// after (sharded, srvMaxHosts)\nmongodb+srv://cluster.example.net/db?srvMaxHosts=3&loadBalanced=true","handlingStrategy":"validation","validationCode":"function validateSrvOptions(opts: { replicaSet?: string; srvMaxHosts?: number }): void {\n  if (opts.replicaSet && (opts.srvMaxHosts ?? 0) > 0) {\n    throw new Error('Cannot combine replicaSet with srvMaxHosts > 0');\n  }\n}\nvalidateSrvOptions({ replicaSet: process.env.REPLICA_SET, srvMaxHosts: Number(process.env.SRV_MAX_HOSTS ?? 0) });","typeGuard":null,"tryCatchPattern":"try {\n  await client.connect();\n} catch (e) {\n  if (e instanceof MongoParseError && /Cannot combine replicaSet option with srvMaxHosts/.test(e.message)) {\n    // drop one of the two and retry, or alert ops\n  }\n  throw e;\n}","preventionTips":["Remember srvMaxHosts is for sharded/LB topologies, not replica sets.","Audit the TXT record for an injected replicaSet when setting srvMaxHosts.","Keep topology-specific URI templates separate."],"tags":["connection-string","srv","replica-set","config"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}