How to Fix MongoDB Error 13436 (NotPrimaryOrSecondary)

Published
Length
3 min read

Luis Felipe Dias

All posts

You get mongodb error 13436 when a client sends a request to a node that is neither primary nor secondary. To fix it, route traffic to healthy replica set members or wait for the node to finish recovery. The database engine marks this failure as NotPrimaryOrSecondary. It belongs to both the NotPrimaryError and RetriableError groups. Your app can retry the operation once the driver finds a ready member in the cluster. What causes mongodb error 13436 in replica sets In a replica set, nodes hol

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You get mongodb error 13436 when a client sends a request to a node that is neither primary nor secondary. To fix it, route traffic to healthy replica set members or wait for the node to finish recovery.

The database engine marks this failure as NotPrimaryOrSecondary. It belongs to both the NotPrimaryError and RetriableError groups. Your app can retry the operation once the driver finds a ready member in the cluster.

What causes mongodb error 13436 in replica sets

In a replica set, nodes hold specific states to serve reads and writes. A primary node takes writes and reads, while secondary nodes serve read requests when allowed. When a node starts up, recovers from a crash, or runs a rollback, it enters a transitional state.

During these states, the node cannot serve normal queries from your app. If a client connects straight to that single host instead of the whole replica set, the request fails. The server rejects the query and returns code 13436 to protect data accuracy.

Network splits can also trigger this error across your cluster. If a node loses contact with the rest of the cluster, it steps down from its role. Until it rejoins the group and catches up on data changes, it blocks normal read and write calls.

Maintenance work is another common cause for this error code. When an operator takes a node offline for storage checks or index work, it leaves the secondary state. Any client pinned to that single host address will fail until the work finishes.

How to diagnose and fix the failure in production

Start by checking how your app connects to the database cluster. Make sure the client uses a full replica set seed list rather than a single node address. When the driver knows the full cluster topology, it routes requests around recovering nodes on its own.

Next, inspect the replica set status to see which member is stuck. Check the server logs on the unhealthy node to see why it left the primary or secondary state. It might be syncing a large batch of data or waiting on disk reads.

MongoDB Authentication & Authorization + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

While you check the cluster, keep your database permissions tight. MongoDB grants access through roles that allow specific actions on defined resources down to the collection level. You do not need full write rights just to inspect cluster health or check collection stats.

For example, the built-in read role grants actions like find, listCollections, listIndexes, dbStats, and collStats on non-system collections. It leaves out data change actions like insert, update, remove, and dropCollection. Using scoped roles lets engineers check database state without risking accidental changes during an outage.

Preventing retry storms during node recovery

Because code 13436 is a RetriableError, modern drivers will try the failed request again. This helps your app survive brief elections or quick restarts without dropping user traffic. Still, you must set sensible retry limits and timeouts in your client setup.

If a whole cluster loses quorum during a network outage, every member might reject queries at once. Without backoff timers, client retries can flood the network and slow down recovery. Set your read preferences and connection pools so apps back off cleanly when nodes are busy.

You should also watch replica lag across all members in your setup. If a secondary falls too far behind the primary, it may enter a recovery state to catch up. Catching disk bottlenecks early stops nodes from dropping out of service during peak traffic hours.

Check MongoDB commands and documents on the wire

MongoDB roles and SCRAM login decide who opens a session. They do not check single commands or redact sensitive fields inside returned documents. With hoop.dev, you run an open-source proxy, a sidecar for your MongoDB, inline in the MongoDB wire protocol. On every command an AI agent or engineer runs against production, hoop.dev decides to pass, mask, block, or hold it for human review.

Before a command runs, hoop.dev guardrails check its effect and refuse banned drops or writes with MongoDB error code 13, or hold risky commands for approval. On the way back, hoop.dev data masking rewrites sensitive fields in returned documents in memory, and every verdict is logged. See the open-source code on GitHub.

Frequently asked questions

What does NotPrimaryOrSecondary mean in MongoDB?

It means the target database node is not in a primary or secondary state right now. The node is likely starting up, syncing data, or recovering from an earlier failure.

Is mongodb error 13436 safe to retry?

Yes, MongoDB classifies code 13436 under the RetriableError and NotPrimaryError categories. A replica set aware driver can retry the request on another healthy node in the cluster.

Why did my read query fail with code 13436?

Even when you allow reads from secondary nodes, a recovering or starting node cannot serve data. Check that your client connects to the replica set rather than one fixed host address.

The query runs. The emails stay hidden.