{"record":{"id":"3840326523192585","repo":"mongodb/node-mongodb-native","slug":"cannot-limit-srv-hosts-with-loadbalanced-enabled","errorCode":null,"errorMessage":"Cannot limit srv hosts with loadBalanced enabled","messagePattern":"Cannot limit srv hosts with loadBalanced enabled","errorType":"exception","errorClass":"MongoParseError","httpStatus":null,"severity":"error","filePath":"src/connection_string.ts","lineNumber":581,"sourceCode":" */\nfunction validateLoadBalancedOptions(\n  hosts: HostAddress[] | string[],\n  mongoOptions: MongoOptions,\n  isSrv: boolean\n): void {\n  if (mongoOptions.loadBalanced) {\n    if (hosts.length > 1) {\n      throw new MongoParseError(LB_SINGLE_HOST_ERROR);\n    }\n    if (mongoOptions.replicaSet) {\n      throw new MongoParseError(LB_REPLICA_SET_ERROR);\n    }\n    if (mongoOptions.directConnection) {\n      throw new MongoParseError(LB_DIRECT_CONNECTION_ERROR);\n    }\n\n    if (isSrv && mongoOptions.srvMaxHosts > 0) {\n      throw new MongoParseError('Cannot limit srv hosts with loadBalanced enabled');\n    }\n  }\n  return;\n}\n\nfunction setOption(\n  mongoOptions: any,\n  key: string,\n  descriptor: OptionDescriptor,\n  values: unknown[]\n) {\n  const { target, type, transform } = descriptor;\n  const name = target ?? key;\n\n  switch (type) {\n    case 'boolean':\n      mongoOptions[name] = getBoolean(name, values[0]);\n      break;","sourceCodeStart":563,"sourceCodeEnd":599,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/connection_string.ts#L563-L599","documentation":"Thrown by MongoParseError at src/connection_string.ts:581 when an SRV URI has `loadBalanced=true` and `srvMaxHosts` set to a positive value. srvMaxHosts would randomly subsample SRV results, but load-balanced mode expects the SRV query to yield the single load-balancer endpoint without sub-sampling.","triggerScenarios":"`mongodb+srv://cluster.example.mongodb.net/?loadBalanced=true&srvMaxHosts=2` or options `{ loadBalanced: true, srvMaxHosts: 2 }` with a +srv URI.","commonSituations":"Trying to combine host-limiting (used for sharded/multi-region tuning) with a load-balanced deployment; copy-paste of an srvMaxHosts-tuned URI onto an LB endpoint.","solutions":["Remove `srvMaxHosts` when running loadBalanced against an SRV hostname.","If you need srvMaxHosts limiting, drop `loadBalanced` and use a non-LB topology."],"exampleFix":"// before\nnew MongoClient('mongodb+srv://cluster.example.mongodb.net/?loadBalanced=true&srvMaxHosts=2')\n\n// after\nnew MongoClient('mongodb+srv://cluster.example.mongodb.net/?loadBalanced=true')","handlingStrategy":"validation","validationCode":"function assertNoLbSrvLimit(uri, options = {}) {\n  const isSrv = uri.startsWith('mongodb+srv://');\n  const lb = options.loadBalanced ?? /loadBalanced=true/i.test(uri);\n  if (isSrv && lb && (options.srvMaxHosts ?? 0) > 0) {\n    throw new Error('srvMaxHosts must be 0/unset for load-balanced SRV connections.');\n  }\n}","typeGuard":null,"tryCatchPattern":"try { new MongoClient(uri, options); } catch (e) { if (e instanceof MongoParseError && /Cannot limit srv hosts with loadBalanced/.test(e.message)) { delete options.srvMaxHosts; } else throw e; }","preventionTips":["Treat srvMaxHosts as a non-LB tuning knob only.","Validate the SRV + loadBalanced + srvMaxHosts triangle in config tests."],"tags":["connection-string","load-balanced","srv","topology"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}