{"id":"69329b59135295fb","repo":"mongodb/node-mongodb-native","slug":"directconnection-option-requires-exactly-one-host","errorCode":null,"errorMessage":"directConnection option requires exactly one host","messagePattern":"directConnection option requires exactly one host","errorType":"exception","errorClass":"MongoParseError","httpStatus":null,"severity":"error","filePath":"src/connection_string.ts","lineNumber":501,"sourceCode":"    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) ||\n    (!mongoOptions.proxyUsername && mongoOptions.proxyPassword)\n  ) {\n    throw new MongoParseError('Can only specify both of proxy username/password or neither');\n  }\n\n  const proxyOptions = ['proxyHost', 'proxyPort', 'proxyUsername', 'proxyPassword'].map(\n    key => urlOptions.get(key) ?? []","sourceCodeStart":483,"sourceCodeEnd":519,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/connection_string.ts#L483-L519","documentation":"Thrown when directConnection is enabled but the URI's host list does not contain exactly one host (connection_string.ts:500-502). directConnection tells the driver to talk to one specific server without SDAM discovery, so multiple hosts are meaningless and zero hosts are impossible by construction.","triggerScenarios":"URI like 'mongodb://h1:27017,h2:27017/?directConnection=true' or a replica-set style URI with multiple hosts plus the directConnection flag.","commonSituations":"Enabling directConnection on a borrowed multi-host URI; building a URI from a host array while always setting directConnection=true.","solutions":["Reduce the URI to exactly one host when using directConnection.","Or remove directConnection to let the driver discover the rest of the topology.","For standalone servers, use 'mongodb://onehost:27017/?directConnection=true'."],"exampleFix":"// before\nconst c = new MongoClient('mongodb://h1:27017,h2:27017/?directConnection=true');\n// after\nconst c = new MongoClient('mongodb://h1:27017/?directConnection=true');","handlingStrategy":"validation","validationCode":"const hostPart = uri.split('://')[1]?.split('/')[0]?.split('?')[0] ?? '';\nconst hostCount = hostPart.split(',').filter(Boolean).length;\nif ((opts.directConnection || new URL(uri).searchParams.get('directConnection') === 'true') && hostCount !== 1) {\n  throw new Error(`directConnection requires exactly one host (got ${hostCount})`);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Generate directConnection URIs from a single host constant.","Avoid combining directConnection with multi-host templates."],"tags":["connection-string","topology","configuration"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}