{"id":"22e4441253a6bd52","repo":"mongodb/node-mongodb-native","slug":"argument-setname-is-required-if-connected-to-a-r","errorCode":null,"errorMessage":"Argument \"setName\" is required if connected to a replica set","messagePattern":"Argument \"setName\" is required if connected to a replica set","errorType":"exception","errorClass":"MongoRuntimeError","httpStatus":null,"severity":"error","filePath":"src/sdam/topology_description.ts","lineNumber":502,"sourceCode":"  const currentAddresses = Array.from(serverDescriptions.keys());\n  const responseAddresses = serverDescription.allHosts;\n  currentAddresses\n    .filter((addr: string) => responseAddresses.indexOf(addr) === -1)\n    .forEach((address: string) => {\n      serverDescriptions.delete(address);\n    });\n\n  return [checkHasPrimary(serverDescriptions), setName, maxSetVersion, maxElectionId];\n}\n\nfunction updateRsWithPrimaryFromMember(\n  serverDescriptions: Map<string, ServerDescription>,\n  serverDescription: ServerDescription,\n  setName: string | null = null\n): TopologyType {\n  if (setName == null) {\n    // TODO(NODE-3483): should be an appropriate runtime error\n    throw new MongoRuntimeError('Argument \"setName\" is required if connected to a replica set');\n  }\n\n  if (\n    setName !== serverDescription.setName ||\n    (serverDescription.me && serverDescription.address !== serverDescription.me)\n  ) {\n    serverDescriptions.delete(serverDescription.address);\n  }\n\n  return checkHasPrimary(serverDescriptions);\n}\n\nfunction updateRsNoPrimaryFromMember(\n  serverDescriptions: Map<string, ServerDescription>,\n  serverDescription: ServerDescription,\n  setName: string | null = null\n): [TopologyType, string | null] {\n  const topologyType = TopologyType.ReplicaSetNoPrimary;","sourceCodeStart":484,"sourceCodeEnd":520,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/sdam/topology_description.ts#L484-L520","documentation":"Thrown by `updateRsWithPrimaryFromMember` during SDAM topology updates when `setName` is null while processing a replica-set member hello (topology_description.ts:500). The driver reached a code path that requires a setName because it believes the topology is a replica set, but no setName was configured. This is treated as a runtime/driver-internal error.","triggerScenarios":"Connecting to a replica set without setting `replicaSet`/`directConnection` correctly while the SDAM logics still classifies the topology as a replica set; an internal call passing null setName.","commonSituations":"Connection string omits `replicaSet=name`; a single-node deployment misdetected as a replica set; driver version mismatch with server SDAM response shape.","solutions":["Add the `replicaSet=<name>` option to your connection string when targeting a replica set.","Update to the latest driver patch release; if it recurs with a correct URI, report a bug with the hello response."],"exampleFix":"// before\nnew MongoClient('mongodb://host1,host2,host3/db');\n// after\nnew MongoClient('mongodb://host1,host2,host3/db?replicaSet=myRs');","handlingStrategy":"validation","validationCode":"function assertRsUri(uri, setName) {\n  if (setName && !/replicaSet=/.test(uri)) {\n    return uri + (uri.includes('?') ? '&' : '?') + 'replicaSet=' + setName;\n  }\n  return uri;\n}","typeGuard":"function hasReplicaSetOption(uri) {\n  return /[?&]replicaSet=/.test(uri);\n}","tryCatchPattern":null,"preventionTips":["Always include replicaSet=<name> in the connection string for replica sets.","Update the driver if the error occurs with a correct URI.","Report persistent occurrences with the server hello response."],"tags":["sdam","internal","replica-set"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}