{"record":{"id":"d972d7fa60f884c9","repo":"mongodb/node-mongodb-native","slug":"srv-uri-does-not-support-directconnection","errorCode":null,"errorMessage":"SRV URI does not support directConnection","messagePattern":"SRV URI does not support directConnection","errorType":"exception","errorClass":"MongoAPIError","httpStatus":null,"severity":"error","filePath":"src/connection_string.ts","lineNumber":473,"sourceCode":"  if (mongoClient && mongoOptions.autoEncryption) {\n    Encrypter.checkForMongoCrypt();\n    mongoOptions.encrypter = new Encrypter(mongoClient, uri, options);\n    mongoOptions.autoEncrypter = mongoOptions.encrypter.autoEncrypter;\n  }\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');","sourceCodeStart":455,"sourceCodeEnd":491,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/connection_string.ts#L455-L491","documentation":"Thrown by MongoAPIError when the URI scheme is `mongodb+srv://` and the `directConnection=true` option is set. SRV records resolve to multiple hosts by design, while directConnection forces a single targeted host — the two are mutually exclusive. Check is at src/connection_string.ts:473 inside the `if (isSRV)` block.","triggerScenarios":"`new MongoClient('mongodb+srv://cluster.example.mongodb.net/?directConnection=true')`, or passing `{ directConnection: true }` options with a +srv URI.","commonSituations":"Debugging connectivity to a single node and reusing the Atlas-style srv URI with `directConnection=true`; template config that sets directConnection globally applied to an Atlas URI.","solutions":["Drop `directConnection=true` from the URI/options when using mongodb+srv://.","If you truly must connect to one host directly, use the plain `mongodb://host:port` scheme with `directConnection=true` instead of the +srv scheme."],"exampleFix":"// before\nnew MongoClient('mongodb+srv://cluster0.example.mongodb.net/?directConnection=true')\n\n// after (single-host direct)\nnew MongoClient('mongodb://node1.example.mongodb.net:27017/?directConnection=true')","handlingStrategy":"validation","validationCode":"function assertNotSrvPlusDirect(uri) {\n  if (uri.startsWith('mongodb+srv://') && /directConnection=true/i.test(uri)) {\n    throw new Error('mongodb+srv:// URIs cannot use directConnection.');\n  }\n}","typeGuard":null,"tryCatchPattern":"try { new MongoClient(uri); } catch (e) { if (e instanceof MongoAPIError && /SRV URI does not support directConnection/.test(e.message)) { uri = uri.replace(/\\??directConnection=true&?/i, ''); } else throw e; }","preventionTips":["Treat directConnection as incompatible with +srv; do not share config flags across them.","When debugging a single node, switch to a plain mongodb:// URI."],"tags":["connection-string","srv","direct-connection","topology"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}