How to Fix MongoDB Error 10107 (NotWritablePrimary)

Published
Length
3 min read

Luis Felipe Dias

All posts

You get mongodb error 10107 when a client sends a write to a non-primary node. You fix it by routing writes to the current replica set primary node. In the database engine, code 10107 has the official name NotWritablePrimary. The engine puts this failure into two groups: NotPrimaryError and RetriableError. Only the primary node in a replica set can take write requests. When a secondary node gets a write request, it rejects the action right away. Because this is a retriable error, a good client

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 10107 when a client sends a write to a non-primary node. You fix it by routing writes to the current replica set primary node.

In the database engine, code 10107 has the official name NotWritablePrimary. The engine puts this failure into two groups: NotPrimaryError and RetriableError. Only the primary node in a replica set can take write requests. When a secondary node gets a write request, it rejects the action right away. Because this is a retriable error, a good client driver can try the write again.

What causes mongodb error 10107 in your cluster

Replica set changes are the main cause of this database error. A primary node might step down during an election or a planned restart. Network splits can also cut off the old primary node from the rest of the cluster. For a few seconds, the old primary becomes a secondary node while nodes vote.

If your app sends an insert or update to that old node, the write fails. Direct connections to a single node host also cause this issue often. When you point an app at one host instead of the whole replica set, it cannot switch. If that one node steps down, every write request hits a secondary node and fails.

How to diagnose and fix NotWritablePrimary safely

Start by checking your replica set status to see which node is the primary. Make sure a majority of nodes are healthy and can reach each other over the network. Next, check the connection string in your app settings. Configure your client driver to use the full replica set topology rather than one host.

When the driver knows the full set, it finds the new primary node on its own. Since code 10107 is a RetriableError, make sure your client driver has retry writes turned on. This lets the driver wait for the election and send the write to the new primary.

Keep database roles tight during incident fixes

When engineers inspect a broken cluster, they often ask for broad database permissions. You do not need write permissions to check why a node stepped down. MongoDB grants access through roles that define allowed actions down to the collection level. The built-in read role grants read access to all non-system collections in its database.

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

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

It includes actions like find, listCollections, listIndexes, dbStats, and collStats for safe checks. It does not allow insert, update, remove, or dropCollection actions at all. Only the readWrite role adds those data modification actions on top of read access. Give engineers read roles for diagnosis so they do not trigger more failed writes.

Prevent manual write errors during live maintenance

Human operators also hit code 10107 when running ad-hoc fix scripts during an outage. If an engineer connects to a secondary node by mistake, any data fix script will fail. Routing human sessions through a central proxy layer helps prevent these connection mistakes. The proxy can point sessions to the right cluster address and enforce role rules.

You can also require peer reviews before someone runs a write action on the primary. Also, data masking can rewrite emails, phone numbers, card numbers, and national IDs in query results. This lets your team check cluster state and fix routing issues with low risk.

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 NotWritablePrimary mean for code 10107?

NotWritablePrimary is the official name for error code 10107. It means a client tried to run a write action on a node that is not the primary.

Can a client driver retry mongodb error 10107 on its own?

Yes, the database marks code 10107 as both a NotPrimaryError and a RetriableError. When your client connects to the full replica set, it can find the new primary and retry.

Do engineers need the readWrite role to check replica set health?

No, the readWrite role is only needed for actions like insert, update, or remove. Engineers can check collection stats and database stats using safe read-only permissions.

The query runs. The emails stay hidden.