{"id":"336d8ea989a6096e","repo":"mongodb/node-mongodb-native","slug":"cannot-use-srvmaxhosts-option-with-replicaset","errorCode":null,"errorMessage":"Cannot use srvMaxHosts option with replicaSet","messagePattern":"Cannot use srvMaxHosts option with replicaSet","errorType":"exception","errorClass":"MongoParseError","httpStatus":null,"severity":"error","filePath":"src/connection_string.ts","lineNumber":477,"sourceCode":"  }\n\n  // Potential SRV Overrides and SRV connection string validations\n\n  mongoOptions.userSpecifiedAuthSource =\n    objectOptions.has('authSource') || urlOptions.has('authSource');\n  mongoOptions.userSpecifiedReplicaSet =\n    objectOptions.has('replicaSet') || urlOptions.has('replicaSet');\n\n  if (isSRV) {\n    // SRV Record is resolved upon connecting\n    mongoOptions.srvHost = hosts[0];\n\n    if (mongoOptions.directConnection) {\n      throw new MongoAPIError('SRV URI does not support directConnection');\n    }\n\n    if (mongoOptions.srvMaxHosts > 0 && typeof mongoOptions.replicaSet === 'string') {\n      throw new MongoParseError('Cannot use srvMaxHosts option with replicaSet');\n    }\n\n    // SRV turns on TLS by default, but users can override and turn it off\n    const noUserSpecifiedTLS = !objectOptions.has('tls') && !urlOptions.has('tls');\n    const noUserSpecifiedSSL = !objectOptions.has('ssl') && !urlOptions.has('ssl');\n    if (noUserSpecifiedTLS && noUserSpecifiedSSL) {\n      mongoOptions.tls = true;\n    }\n  } else {\n    const userSpecifiedSrvOptions =\n      urlOptions.has('srvMaxHosts') ||\n      objectOptions.has('srvMaxHosts') ||\n      urlOptions.has('srvServiceName') ||\n      objectOptions.has('srvServiceName');\n\n    if (userSpecifiedSrvOptions) {\n      throw new MongoParseError(\n        'Cannot use srvMaxHosts or srvServiceName with a non-srv connection string'","sourceCodeStart":459,"sourceCodeEnd":495,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/connection_string.ts#L459-L495","documentation":"Thrown when an SRV URI is used, srvMaxHosts is greater than 0, and a replicaSet name is also specified (connection_string.ts:476-478). srvMaxHosts randomly limits the hosts returned by SRV to introduce randomization, but pinning a replicaSet name requires connecting to all members for election-aware routing; the two are mutually exclusive.","triggerScenarios":"URI like 'mongodb+srv://cluster.example/?srvMaxHosts=2&replicaSet=rs0' or options { srvMaxHosts: 2, replicaSet: 'rs0' } with a mongodb+srv:// URI.","commonSituations":"Adding srvMaxHosts to reduce connection fan-out on a large global cluster while retaining an explicit replicaSet name from older config.","solutions":["Remove the replicaSet option (let SRV + SDAM discover the set).","Or set srvMaxHosts=0 (the default) to keep the explicit replicaSet.","Prefer relying on SRV-record topology discovery for Atlas-style clusters."],"exampleFix":"// before\nconst c = new MongoClient('mongodb+srv://cluster.example/?srvMaxHosts=2&replicaSet=rs0');\n// after\nconst c = new MongoClient('mongodb+srv://cluster.example/?srvMaxHosts=2');","handlingStrategy":"validation","validationCode":"const isSRV = uri.startsWith('mongodb+srv://');\nconst srvMaxHosts = Number(opts.srvMaxHosts ?? new URL(uri).searchParams.get('srvMaxHosts') ?? 0);\nconst hasRS = Boolean(opts.replicaSet ?? new URL(uri).searchParams.get('replicaSet'));\nif (isSRV && srvMaxHosts > 0 && hasRS) {\n  throw new Error('Cannot combine srvMaxHosts > 0 with replicaSet on an SRV URI');\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Decide between replicaSet pinning and srvMaxHosts randomization; don't combine.","Document the topology strategy in your connection module."],"tags":["connection-string","srv","topology","replica-set"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}