NotWritablePrimary: not primary and secondaryOk=false MongoDB Origin: Data OSI layer 7 · Application Last updated 5 Nov 2026

MongoServerError: not primary and secondaryOk=false / NotWritablePrimary: not master

A write (or read without secondary preference) hit a secondary — the primary changed or you connected directly to a secondary.

Exact message you may see not primary and secondaryOk=falseMongoServerError: not primaryNotWritablePrimary: not masternot master and slaveOk=false
Seen on: MongoDB

Meaning

Connect with the replica set connection string (all hosts + replicaSet=rs0) so the driver finds the primary; direct connections to a secondary can't write. After failover, writes retry (retryWrites=true). Check rs.status() for the current primary.

Common causes

  • Connecting directly to a secondary (directConnection=true)
  • Failover/election in progress
  • Replica set name missing in the URI
  • Hostnames not resolvable from the client
  • Stale DNS for replica members

⚡ Quick fix

  1. Use the full URI: mongodb://h1,h2,h3/db?replicaSet=rs0
  2. Remove directConnection=true
  3. Check rs.status() and retry after the election

Detailed fix by platform

mongosh

  1. rs.status().members.map(m => [m.name, m.stateStr])

How to diagnose

  1. URI — All members + replicaSet?
  2. Direct — directConnection?
  3. Election — In progress?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Data. Errors that are often confused with or caused by this one (click to open):

AlreadyInitialized: already initialized — MongoServerError: already initialized (rs.initiate on a configured replica set) (MongoDB)AlreadyInitialized: a…Could not find host matching read preference — MongoServerSelectionError: Could not find host matching read preference { mode: "secondary" } for set rs0 (MongoDB)Could not find host m…No primary found in replica set / ReplicaSetNoPrimary — MongoServerSelectionError: Server selection timed out after 30000 ms — topology type ReplicaSetNoPrimary (MongoDB)No primary found in r…NoSuchTransaction: Transaction was aborted or expired — MongoServerError: Transaction 1 has been aborted / NoSuchTransaction: transaction has expired (transactionLifetimeLimitSeconds) (MongoDB)NoSuchTransaction: Tr…READONLY You can't write against a read only replica — Redis: READONLY You can't write against a read only replica (Redis)READONLY You can't wr…rs.initiate failed: member not reachable / no host described in new configuration — MongoDB: replSetInitiate quorum check failed: Could not connect to mongo2:27017 / No host described in new configuration 1 for replica set rs0 maps to this node (MongoDB)rs.initiate failed: m…Secondary too stale to catch up (RECOVERING) — MongoDB: We are too stale to use X as a sync source / replSet too stale to catch up — member in RECOVERING state (MongoDB)Secondary too stale t…Transaction numbers are only allowed on a replica set member or mongos — MongoServerError: Transaction numbers are only allowed on a replica set member or mongos (MongoDB)Transaction numbers a…NotWritab…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.