{"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/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/connection_string.ts#L556-L592","documentation":"Thrown by validateLoadBalancedOptions (connection_string.ts:573-575) when both loadBalanced=true and a replicaSet name are provided. Load-balanced topology hides individual servers behind a single endpoint, while replicaSet requires direct member awareness for elections; the combination is invalid.","triggerScenarios":"URI like 'mongodb://lb.example/?loadBalanced=true&replicaSet=rs0' or options { replicaSet: 'rs0' } combined with a loadBalanced URI.","commonSituations":"Migrating from a replica-set URI to a load-balanced deployment and leaving the replicaSet parameter; Atlas-style templated config that injects replicaSet by default.","solutions":["Remove the replicaSet option when using loadBalanced.","Or remove loadBalanced if you intend to connect directly to the replica set.","Pick one topology model and align all options to it."],"exampleFix":"// before\nconst c = new MongoClient('mongodb://lb.example/?loadBalanced=true&replicaSet=rs0');\n// after\nconst c = new MongoClient('mongodb://lb.example/?loadBalanced=true');","handlingStrategy":"validation","validationCode":"const lb = new URL(uri).searchParams.get('loadBalanced') === 'true' || opts.loadBalanced === true;\nconst hasRS = Boolean(opts.replicaSet ?? new URL(uri).searchParams.get('replicaSet'));\nif (lb && hasRS) throw new Error('loadBalanced and replicaSet are mutually exclusive');","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Decide topology mode (replica set vs load balanced) up front and template options accordingly.","Run a preflight check on assembled connection config."],"tags":["connection-string","load-balancing","replica-set","topology"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}