How to Fix MongoDB Error 13435 (NotPrimaryNoSecondaryOk)
You get mongodb error 13435 when a client reads from a secondary node without permission. You fix it by routing traffic to the primary node or allowing secondary reads. MongoDB names this failure NotPrimaryNoSecondaryOk in its official error list. It belongs to both the NotPrimaryError and RetriableError groups. Production apps hit this issue during replica set elections or network shifts. Knowing why the database blocks the query helps your team restore service fast. What causes mongodb erro
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 13435 when a client reads from a secondary node without permission. You fix it by routing traffic to the primary node or allowing secondary reads.
MongoDB names this failure NotPrimaryNoSecondaryOk in its official error list. It belongs to both the NotPrimaryError and RetriableError groups. Production apps hit this issue during replica set elections or network shifts. Knowing why the database blocks the query helps your team restore service fast.
What causes mongodb error 13435 in replica sets
A MongoDB replica set holds one primary node and multiple secondary nodes. By default, the database sends all reads and writes to the primary node. If a client talks directly to a secondary node, that node rejects normal reads. The secondary node then returns code 13435 because secondary reads are off.
This problem often starts during a planned maintenance window or an unexpected node crash. The primary node steps down and becomes a secondary node. Connected apps that still send read requests to that old host get rejected right away.
Hardcoded hostnames in client connection strings also trigger this error code. If an engineer points a tool at one node instead of the full replica set, elections break the setup. When that single host changes state, the client cannot find the new primary node.
Network splits can cause the exact same failure mode. If a primary node loses contact with the majority of the cluster, it steps down. Clients on the wrong side of the network split keep querying that stepped-down node. Because the node is no longer primary, it throws the NotPrimaryNoSecondaryOk error.
How to diagnose and fix NotPrimaryNoSecondaryOk safely
Start by checking the current state of your replica set nodes. Identify which host holds the primary role and which hosts act as secondaries. Because code 13435 is a RetriableError, modern drivers can often recover on their own. Make sure your app connects using a full replica set connection string.
A complete connection string lists several cluster hosts instead of just one machine. This lets the client driver discover the new primary node after an election finishes. If the client only runs reporting queries, you can also adjust its read preference. Setting the client to allow secondary reads stops code 13435 on replica nodes.
MongoDB Authentication & Authorization + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Be careful not to grant extra database permissions while you debug this failure. Teams under pressure sometimes hand out write roles just to make an error stop. Role settings and node routing are two separate layers in MongoDB.
MongoDB grants access through role-based authorization on a specific database down to the collection level. The built-in read role grants read access to all non-system collections. It includes actions like find, listCollections, listIndexes, dbStats, collStats, and changeStream. The readWrite role adds data changes like insert, update, remove, createIndex, and dropCollection.
Keep human users and reporting tools on the read role during your fix. Changing a user from read to readWrite will not fix a secondary routing mistake. Fix the connection target or read preference instead of widening user privileges.
Preventing repeat errors during cluster elections
You can stop repeat outages by reviewing how engineers and apps reach the cluster. Remove direct single-host connections from scripts, dashboards, and internal tools. Always use cluster-aware routing so traffic follows the primary node when topology changes.
Test your app behavior by stepping down the primary node in a staging setup. Watch how your client drivers handle the RetriableError response during the brief election window. Good retry logic and proper read preferences keep short elections from turning into user outages.
Inspect MongoDB wire traffic in flight
Built-in MongoDB roles govern collection access at setup time. They cannot score command risk per call or mask sensitive document values on the fly. By design, hoop.dev sits inline in the MongoDB protocol next to your server as a sidecar for your MongoDB. On every production command from mongosh, drivers, or AI agents, hoop.dev decides whether to pass, mask, block, or hold it for human review.
First, hoop.dev guardrails block bad deletes, drops, and write pipelines or hold them for approval. Next, hoop.dev data masking rewrites sensitive values in returned documents in memory and logs each decision. Explore the repo on GitHub.
Frequently asked questions
What does NotPrimaryNoSecondaryOk mean in MongoDB?
It means a client sent a read query to a secondary replica set node. The node blocked the query because the client did not allow secondary reads.
Is mongodb error 13435 safe to retry automatically?
Yes, MongoDB marks code 13435 as both a NotPrimaryError and a RetriableError. Client drivers that know the full replica set can retry the read on the primary node.
Does the readWrite role fix error 13435 on a secondary node?
No, roles like read and readWrite only control what actions a user can run. You must route the client to the primary node or enable secondary read preferences.
Related guides
In this post