{"record":{"id":"f6c55f83fb5dbf71","repo":"mongodb/node-mongodb-native","slug":"cannot-use-srvmaxhosts-or-srvservicename-with-a-no","errorCode":null,"errorMessage":"Cannot use srvMaxHosts or srvServiceName with a non-srv connection string","messagePattern":"Cannot use srvMaxHosts or srvServiceName with a non-srv connection string","errorType":"exception","errorClass":"MongoParseError","httpStatus":null,"severity":"error","filePath":"src/connection_string.ts","lineNumber":494,"sourceCode":"    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'\n      );\n    }\n  }\n\n  if (mongoOptions.directConnection && mongoOptions.hosts.length !== 1) {\n    throw new MongoParseError('directConnection option requires exactly one host');\n  }\n\n  if (\n    !mongoOptions.proxyHost &&\n    (mongoOptions.proxyPort || mongoOptions.proxyUsername || mongoOptions.proxyPassword)\n  ) {\n    throw new MongoParseError('Must specify proxyHost if other proxy options are passed');\n  }\n\n  if (\n    (mongoOptions.proxyUsername && !mongoOptions.proxyPassword) ||","sourceCodeStart":476,"sourceCodeEnd":512,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/connection_string.ts#L476-L512","documentation":"Thrown by MongoParseError (else branch at src/connection_string.ts:494) when the URI is plain `mongodb://` (not +srv) but the user supplied `srvMaxHosts` or `srvServiceName`. Those options only have meaning for SRV record resolution, so a non-SRV connection has nothing for them to do.","triggerScenarios":"`mongodb://host1:27017,host2:27017/?srvMaxHosts=2`, or `mongodb://host:27017/?srvServiceName=_tcp.example.com`, or passing either option via the options object with a plain mongodb:// URI.","commonSituations":"Switching a URI from `mongodb+srv://` to `mongodb://` but forgetting to drop the srv-only options; sharing an options object across srv and non-srv clients.","solutions":["Switch the URI scheme to `mongodb+srv://` if you want SRV-record-driven discovery.","Otherwise remove `srvMaxHosts` and `srvServiceName` from both the URI query string and the options object."],"exampleFix":"// before\nnew MongoClient('mongodb://host1:27017,host2:27017/?srvMaxHosts=2')\n\n// after (plain mongodb, no srv option)\nnew MongoClient('mongodb://host1:27017,host2:27017/')","handlingStrategy":"validation","validationCode":"function assertSrvOptionsMatchScheme(uri, options = {}) {\n  const isSrv = uri.startsWith('mongodb+srv://');\n  if (!isSrv && (options.srvMaxHosts != null || options.srvServiceName || /[?&]srv(MaxHosts|ServiceName)=/.test(uri))) {\n    throw new Error('srvMaxHosts / srvServiceName require a mongodb+srv:// URI.');\n  }\n}","typeGuard":null,"tryCatchPattern":"try { new MongoClient(uri, options); } catch (e) { if (e instanceof MongoParseError && /srvMaxHosts or srvServiceName with a non-srv/.test(e.message)) { delete options.srvMaxHosts; delete options.srvServiceName; } else throw e; }","preventionTips":["When switching between +srv and plain mongodb:// URIs, scrub srvMaxHosts and srvServiceName from the config.","Keep a single source of truth for the URI scheme so dependent options can be derived from it."],"tags":["connection-string","srv","topology"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}