{"id":"05b6b6e1ca07e0a8","repo":"mongodb/node-mongodb-native","slug":"selected-server-does-not-support-retryable-writes","errorCode":null,"errorMessage":"Selected server does not support retryable writes","messagePattern":"Selected server does not support retryable writes","errorType":"exception","errorClass":"MongoUnexpectedServerResponseError","httpStatus":null,"severity":"error","filePath":"src/operations/execute_operation.ts","lineNumber":360,"sourceCode":"        (operationError.hasErrorLabel(MongoErrorLabel.SystemOverloadedError) &&\n          topology.s.options.enableOverloadRetargeting)\n      ) {\n        deprioritizedServers.add(server.description);\n      }\n\n      server = await topology.selectServer(selector, {\n        session,\n        operationName: operation.commandName,\n        deprioritizedServers,\n        signal: operation.options.signal\n      });\n\n      if (\n        hasWriteAspect &&\n        !supportsRetryableWrites(server) &&\n        !operationError.hasErrorLabel(MongoErrorLabel.SystemOverloadedError)\n      ) {\n        throw new MongoUnexpectedServerResponseError(\n          'Selected server does not support retryable writes'\n        );\n      }\n\n      // Batched operations must reset the batch before retry,\n      // otherwise building a command will build the _next_ batch, not the current batch.\n      if (operation.hasAspect(Aspect.COMMAND_BATCHING)) {\n        operation.resetBatch();\n      }\n    }\n  }\n\n  throw (\n    error ??\n    new MongoRuntimeError(\n      'Should never happen: operation execution loop terminated but no error was recorded.'\n    )\n  );","sourceCodeStart":342,"sourceCodeEnd":378,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/operations/execute_operation.ts#L342-L378","documentation":"During retry inside executeOperationWithRetries (src/operations/execute_operation.ts:360), after re-selecting a server, the driver checks supportsRetryableWrites(server). If the new server does not support retryable writes and the error is not a SystemOverloadedError, a MongoUnexpectedServerResponseError is thrown. This typically happens when a failover lands the operation on a standalone or otherwise incompatible server.","triggerScenarios":"A retryable write fails on one server, the driver re-selects, and the newly selected server (e.g. a standalone promoted during a topology change, or a server that lost its replica-set membership) does not support retryable writes.","commonSituations":"Replica set converting to standalone, an election that leaves a non-retryable-capable server selected, or a topology misconfiguration in a sharded cluster where a mongos does not report retryable-writes support.","solutions":["Verify the deployment topology is a healthy replica set or sharded cluster (not standalone) when using retryable writes.","If the deployment genuinely cannot support retryable writes, set retryWrites=false.","Check cluster health and re-run; transient failovers may resolve on the next attempt.","Inspect server hello output to confirm `retryableWrite` capability on all members."],"exampleFix":"// before\nconst client = new MongoClient('mongodb://localhost'); // standalone, retryWrites defaults true\nawait coll.updateOne({}, { $set: { a: 1 } }); // after a failover → throws\n\n// after\nconst client = new MongoClient('mongodb://localhost/?retryWrites=false');\n// or deploy as a replica set so all members support retryable writes","handlingStrategy":"retry","validationCode":"// Confirm all cluster members report retryable write support\nconst hello = await client.db().admin().command({ hello: 1 });\nif (!hello.retryableWrite) {\n  // set retryWrites=false or fix topology\n}","typeGuard":null,"tryCatchPattern":"try {\n  await collection.updateOne(filter, update);\n} catch (err) {\n  if (err instanceof MongoUnexpectedServerResponseError && /retryable writes/.test(err.message)) {\n    // wait for topology to stabilize, then retry once; otherwise disable retryWrites\n  } else throw err;\n}","preventionTips":["Ensure the deployment is a healthy replica set or sharded cluster, not standalone.","Set retryWrites=false if the topology genuinely lacks support.","Monitor elections and failovers that can change server capabilities."],"tags":["retryable-writes","failover","topology","server-selection"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}