{"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/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/connection_string.ts#L476-L512","documentation":"Thrown in the non-SRV branch (connection_string.ts:486-498) when a plain mongodb:// URI is given but srvMaxHosts or srvServiceName appears in either the URI query string or the options object. These options only make sense for mongodb+srv:// URIs because they parameterize the SRV DNS lookup.","triggerScenarios":"URI like 'mongodb://host:27017/?srvMaxHosts=3' or new MongoClient('mongodb://host:27017/', { srvServiceName: 'customservice' }).","commonSituations":"Switching a URI from mongodb+srv:// to mongodb:// for testing or firewalls but leaving the SRV-only options in place; templated config that injects srv options unconditionally.","solutions":["Remove srvMaxHosts and srvServiceName from URI and options.","Or change the URI scheme to mongodb+srv:// if you genuinely want SRV resolution."],"exampleFix":"// before\nconst c = new MongoClient('mongodb://host:27017/?srvMaxHosts=3');\n// after\nconst c = new MongoClient('mongodb://host:27017/');\n// or\nconst c = new MongoClient('mongodb+srv://cluster.example/?srvMaxHosts=3');","handlingStrategy":"validation","validationCode":"if (!uri.startsWith('mongodb+srv://')) {\n  const params = new URL(uri).searchParams;\n  if (params.has('srvMaxHosts') || params.has('srvServiceName') || 'srvMaxHosts' in opts || 'srvServiceName' in opts) {\n    throw new Error('srvMaxHosts/srvServiceName require a mongodb+srv:// URI');\n  }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep SRV-only options grouped with mongodb+srv:// URIs in config.","Add a CI test that asserts option/scheme pairing."],"tags":["connection-string","srv","configuration"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}