{"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/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/connection_string.ts#L455-L491","documentation":"Thrown (as MongoAPIError) inside the isSRV branch (connection_string.ts:472-474) when a mongodb+srv:// URI is combined with directConnection=true. SRV records resolve to multiple hosts and the driver uses topology discovery; directConnection pins to a single host and is semantically incompatible with SRV.","triggerScenarios":"URI like 'mongodb+srv://cluster.example/?directConnection=true' or new MongoClient('mongodb+srv://...', { directConnection: true }).","commonSituations":"Switching a single-host URI to SRV for managed Atlas clusters but keeping directConnection for a side task; misreading documentation that recommends directConnection for standalone instances.","solutions":["Remove directConnection from the SRV URI/options.","If you truly need a direct single-host connection, use a standard mongodb:// URI with the explicit host:port instead of mongodb+srv://.","Use directConnection only with mongodb:// URIs pointing at one host."],"exampleFix":"// before\nconst c = new MongoClient('mongodb+srv://cluster.example/?directConnection=true');\n// after\nconst c = new MongoClient('mongodb+srv://cluster.example/');\n// or, for a direct single-host connection:\nconst c = new MongoClient('mongodb://node1.example:27017/?directConnection=true');","handlingStrategy":"validation","validationCode":"if (uri.startsWith('mongodb+srv://') && (opts.directConnection || new URL(uri).searchParams.get('directConnection') === 'true')) {\n  throw new Error('SRV URIs cannot use directConnection');\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Reserve directConnection for plain mongodb:// single-host debugging.","Validate URI scheme vs topology options in a connection-config helper."],"tags":["connection-string","srv","topology","configuration"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}