How to Fix MongoDB Error 10058 (LegacyNotPrimary)
You get mongodb error 10058 when a client sends a write or primary read to a secondary node. You can fix this LegacyNotPrimary error by routing traffic to the primary node or updating client read settings. In a replica set, only one node acts as the primary at any given time. That primary node takes all write requests from your apps and users. Secondary nodes copy data from the primary to stay in sync. When a network split or election happens, the primary can step down and become a secondary. I
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 10058 when a client sends a write or primary read to a secondary node. You can fix this LegacyNotPrimary error by routing traffic to the primary node or updating client read settings.
In a replica set, only one node acts as the primary at any given time. That primary node takes all write requests from your apps and users. Secondary nodes copy data from the primary to stay in sync. When a network split or election happens, the primary can step down and become a secondary. If your app still points to that old node, the database rejects the command with error code 10058.
What causes mongodb error 10058 in replica sets
The official MongoDB error name for code 10058 is LegacyNotPrimary. It happens inside the database engine when a member is not in the primary state. Three common events trigger this failure in production systems.
First, a replica set election may move the primary role to a different server. Your client app might hold an old connection to a single host instead of the whole cluster. When the old host steps down, every write to that host fails right away.
Second, a developer or tool might connect to a secondary node on purpose to inspect data. By default, the database blocks reads on secondary nodes unless the client allows secondary reads. If the client runs a query without that setting, the node returns code 10058.
Third, maintenance tasks or high load can force a node into a recovery state. While a node recovers or syncs, it cannot serve as the primary member. Any direct traffic to that node will fail until the cluster finishes its election.
How to diagnose and fix LegacyNotPrimary in production
To fix this error, check how your app connects to the database cluster. Make sure your client uses a replica set connection string that lists multiple hosts. When you list the full set, the driver can find the new primary after an election.
MongoDB Authentication & Authorization + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Next, check which node holds the primary state right now. You can run a status check on the replica set to see each member and its role. If no primary exists, check the network links between your database servers. A split network stops nodes from picking a new leader.
When engineers inspect a live issue, do not grant broad write access to fix connection errors. MongoDB grants access through role-based rules that apply to a specific database down to the collection level. The built-in read role grants actions like find, listCollections, listIndexes, dbStats, collStats, and changeStream on non-system collections. It leaves out actions that change data, such as insert, update, remove, and dropCollection.
If an engineer only needs to check data on a secondary node, assign the read role. Then set the client session to allow reads from secondary members. Save the readWrite role for apps and tasks that must insert, update, remove, or manage indexes on the primary.
How to prevent node routing errors during incidents
Direct connections to single database nodes often break during routine cluster failovers. Teams often share static host addresses in tickets or chat channels during an outage. Those host addresses go stale as soon as the replica set elects a new primary.
Instead of connecting to single host addresses, route user and app sessions through cluster-aware endpoints. Keep read and write roles split so read-only queries can run on secondaries without risk. Also, use data masking on query results to rewrite emails, phone numbers, card numbers, and national IDs.
Add command guardrails and field masking beside MongoDB
Collection roles and IP rules control who connects to MongoDB. Yet an active client or agent session can still run a pipeline that writes data or reads raw personal fields. In practice, hoop.dev sits inline in the wire protocol beside your database as a sidecar for your MongoDB. For every command an agent or human sends in production, hoop.dev decides to pass, mask, block, or escalate it for review.
Using hoop.dev MongoDB controls, guardrails refuse banned commands before they run or hold risky calls for approval, and data masking rewrites matched fields in returned documents in memory. Furthermore, hoop.dev holds SOC 2 Type II, is MIT licensed, and generates evidence for GDPR, HIPAA, and PCI DSS programs. View the project on GitHub.
Frequently asked questions
What does LegacyNotPrimary mean in MongoDB?
LegacyNotPrimary is the official name for mongodb error 10058. It means a client sent an operation to a replica set member that is not the primary node.
Can a read query trigger mongodb error 10058?
Yes, a read query triggers this error if you connect to a secondary node with default read settings. You must enable secondary read preference on your client session to read from a secondary node.
Does granting the readWrite role fix error 10058?
No, changing user roles does not fix node routing errors. The readWrite role adds insert, update, and remove actions, but writes still fail on secondary nodes.
Related guides
In this post