{"record":{"id":"5e9972b2ee3021fa","repo":"mongodb/node-mongodb-native","slug":"loadbalanced-option-not-supported-with-a-replicase","errorCode":null,"errorMessage":"loadBalanced option not supported with a replicaSet option","messagePattern":"loadBalanced option not supported with a replicaSet option","errorType":"exception","errorClass":"MongoParseError","httpStatus":null,"severity":"error","filePath":"src/connection_string.ts","lineNumber":574,"sourceCode":" * #### Throws if LB mode is true:\n * - hosts contains more than one host\n * - there is a replicaSet name set\n * - directConnection is set\n * - if srvMaxHosts is used when an srv connection string is passed in\n *\n * @throws MongoParseError\n */\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) {","sourceCodeStart":556,"sourceCodeEnd":592,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/connection_string.ts#L556-L592","documentation":"Thrown by MongoParseError (constant LB_REPLICA_SET_ERROR) at src/connection_string.ts:574 when `loadBalanced=true` and `replicaSet` is also set. A load-balanced topology deliberately hides the backend topology behind one endpoint, so naming a replica set contradicts that model.","triggerScenarios":"`mongodb://host:27017/?loadBalanced=true&replicaSet=rs0`, or options `{ loadBalanced: true, replicaSet: 'rs0' }`.","commonSituations":"Migrating from a replica-set URI to a load-balanced LB/mongos endpoint and forgetting to drop the replicaSet option; shared config object that always sets replicaSet.","solutions":["Remove `replicaSet` from the URI and options when running in loadBalanced mode.","If you actually want replica-set semantics, remove `loadBalanced`."],"exampleFix":"// before\nnew MongoClient('mongodb://host:27017/?loadBalanced=true&replicaSet=rs0')\n\n// after\nnew MongoClient('mongodb://host:27017/?loadBalanced=true')","handlingStrategy":"validation","validationCode":"function assertNoLbPlusRs(options = {}, uri = '') {\n  const lb = options.loadBalanced ?? /loadBalanced=true/i.test(uri);\n  const rs = options.replicaSet ?? uri.match(/replicaSet=([^&]+)/i)?.[1];\n  if (lb && rs) throw new Error('loadBalanced and replicaSet cannot both be set.');\n}","typeGuard":null,"tryCatchPattern":"try { new MongoClient(uri, options); } catch (e) { if (e instanceof MongoParseError && /loadBalanced option not supported with a replicaSet/.test(e.message)) { delete options.replicaSet; } else throw e; }","preventionTips":["When migrating to a load-balanced deployment, scrub replicaSet from inherited config.","Treat loadBalanced and replicaSet as mutually exclusive topology modes."],"tags":["connection-string","load-balanced","replica-set","topology"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}