# MongoServerError: no primary found in replica set, cannot perform operation

- **ID:** `database/mongodb-replica-set-no-primary`
- **Domain:** database
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

No member of the MongoDB replica set is in PRIMARY state, often due to an election failure, network partition, or majority loss.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| MongoDB 6.0 | active | — | — |
| MongoDB 7.0 | active | — | — |
| MongoDB 8.0 | active | — | — |

## Workarounds

1. **Check replica set status: rs.status(); identify which nodes are up and their states. If a node is SECONDARY but has high replication lag, wait for it to catch up.** (80% success)
   ```
   Check replica set status: rs.status(); identify which nodes are up and their states. If a node is SECONDARY but has high replication lag, wait for it to catch up.
   ```
2. **Force re-election by connecting to a secondary and running: rs.reconfigForPSASet(); or manually reconfigure with rs.reconfig() to set a new primary.** (75% success)
   ```
   Force re-election by connecting to a secondary and running: rs.reconfigForPSASet(); or manually reconfigure with rs.reconfig() to set a new primary.
   ```
3. **If a node is in ROLLBACK state, allow rollback to complete: use rs.stepDown() on the former primary to let the rollback finish.** (85% success)
   ```
   If a node is in ROLLBACK state, allow rollback to complete: use rs.stepDown() on the former primary to let the rollback finish.
   ```

## Dead Ends

- **Restart all MongoDB instances in the replica set simultaneously** — Restarting all nodes at once can cause a complete outage and may not resolve underlying election issues; a step restart is safer. (70% fail)
- **Forcibly step down a secondary with rs.stepDown() to trigger election** — If no qualified secondary exists (e.g., all are behind in replication), stepDown() will not elect a new primary. (60% fail)
