{
  "id": "mongodb/replica-set-election-hiccup",
  "signature": "MongoServerError: not master and slaveOk=false",
  "signature_zh": "MongoServerError：不是主节点且 slaveOk=false",
  "regex": "not master and slaveOk=false",
  "domain": "mongodb",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "A secondary replica set member received a write operation or a read without slaveOk enabled during an election.",
  "root_cause_type": "generic",
  "root_cause_zh": "在选举期间，从节点收到了写操作或未启用 slaveOk 的读操作。",
  "versions": [
    {
      "version": "mongodb-3.6",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "mongodb-4.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "mongodb-4.2",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "mongodb-4.4",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "mongodb-5.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "mongodb-6.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "mongodb-7.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "This bypasses read preference and may cause stale reads; it does not fix write failures during elections.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Restarting does not resolve the transient election state; it may cause longer downtime.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Without proper retry logic with backoff, this can overwhelm the server during elections.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Implement automatic retry with exponential backoff for write operations, and use readPreference=secondaryPreferred for reads. Example in Node.js: await collection.insertOne(doc, { w: 'majority', wtimeout: 5000 }).catch(e => { if (e.codeName === 'NotMaster') { /* retry after 1s */ } })",
      "success_rate": 0.85,
      "how": "Implement automatic retry with exponential backoff for write operations, and use readPreference=secondaryPreferred for reads. Example in Node.js: await collection.insertOne(doc, { w: 'majority', wtimeout: 5000 }).catch(e => { if (e.codeName === 'NotMaster') { /* retry after 1s */ } })",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use MongoDB driver's built-in retryWrites and retryReads options (enabled by default in 4.2+). Configure connection string: mongodb://host1,host2,host3/?retryWrites=true&retryReads=true",
      "success_rate": 0.9,
      "how": "Use MongoDB driver's built-in retryWrites and retryReads options (enabled by default in 4.2+). Configure connection string: mongodb://host1,host2,host3/?retryWrites=true&retryReads=true",
      "condition": "",
      "sources": []
    },
    {
      "action": "Ensure at least 3 voting members in the replica set to avoid elections causing extended unavailability.",
      "success_rate": 0.75,
      "how": "Ensure at least 3 voting members in the replica set to avoid elections causing extended unavailability.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Implement automatic retry with exponential backoff for write operations, and use readPreference=secondaryPreferred for reads. Example in Node.js: await collection.insertOne(doc, { w: 'majority', wtimeout: 5000 }).catch(e => { if (e.codeName === 'NotMaster') { /* retry after 1s */ } })",
    "Use MongoDB driver's built-in retryWrites and retryReads options (enabled by default in 4.2+). Configure connection string: mongodb://host1,host2,host3/?retryWrites=true&retryReads=true",
    "Ensure at least 3 voting members in the replica set to avoid elections causing extended unavailability."
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://www.mongodb.com/docs/manual/reference/method/rs.slaveOk/",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.85,
  "fix_success_rate": 0.85,
  "resolvable": "true",
  "first_seen": "2023-06-15",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}