MongoNotPrimaryException (not master)

I am connecting to mongodb replicaset where I have write concern = 2 and readPreference is PrimaryOnly. I am seeing a lot of this exceptions currently.


  1. When I connect to a mongodb do I need to give the hostnames of all the mongo servers or just 1 is enough ?

  2. This exception is coming for a write operation will it also come for read operation

Here is the exception

com.mongodb.MongoNotPrimaryException: Command failed with error 10107 (NotMaster): 'not master' on server xxx.hostname:port . The full response is {"operationTime": {"$timestamp": {"t": 1595902580, "i": 1}}, "ok": 0.0, "errmsg": "not master", "code": 10107, "codeName": "NotMaster", "$clusterTime": {"clusterTime": {"$timestamp": {"t": 1595902580, "i": 1}}, "signature": {"hash": {"$binary": {"base64": "CtMA9l2pphTu5U2AmiPo1MgOmn0=", "subType": "00"}}, "keyId": 6803004021045985281}}}
        at com.mongodb.internal.connection.ProtocolHelper.createSpecialException( ~[mongodb-driver-core-4.0.4.jar!/:?]
        at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException( ~[mongodb-driver-core-4.0.4.jar!/:?]
        at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse( ~[mongodb-driver-core-4.0.4.jar!/:?]
        at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive( ~[mongodb-driver-core-4.0.4.jar!/:?]
        at com.mongodb.internal.connection.UsageTrackingInternalConnection.sendAndReceive( ~[mongodb-driver-core-4.0.4.jar!/:?]
        at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection.sendAndReceive( ~[mongodb-driver-core-4.0.4.jar!/:?]
        at com.mongodb.internal.connection.CommandProtocolImpl.execute( ~[mongodb-driver-core-4.0.4.jar!/:?]
        at com.mongodb.internal.connection.DefaultServer$DefaultServerProtocolExecutor.execute( ~[mongodb-driver-core-4.0.4.jar!/:?]
        at com.mongodb.internal.connection.DefaultServerConnection.executeProtocol( ~[mongodb-driver-core-4.0.4.jar!/:?]
        at com.mongodb.internal.connection.DefaultServerConnection.command( ~[mongodb-driver-core-4.0.4.jar!/:?]
        at com.mongodb.internal.connection.DefaultServerConnection.command( ~[mongodb-driver-core-4.0.4.jar!/:?]
        at com.mongodb.internal.operation.CommandOperationHelper.executeWriteCommand( ~[mongodb-driver-core-4.0.4.jar!/:?]
        at com.mongodb.internal.operation.CommandOperationHelper.executeWriteCommand( ~[mongodb-driver-core-4.0.4.jar!/:?]
        at com.mongodb.internal.operation.CommandOperationHelper.executeCommand( ~[mongodb-driver-core-4.0.4.jar!/:?]
        at com.mongodb.internal.operation.CommandOperationHelper.executeCommand( ~[mongodb-driver-core-4.0.4.jar!/:?]
        at com.mongodb.internal.operation.CreateIndexesOperation$ ~[mongodb-driver-core-4.0.4.jar!/:?]
1 Like


  1. When I connect to a mongodb do I need to give the hostnames of all the mongo servers or just 1 is enough ?

It is a good practice to specify all host names of the replica-set in the connection string. An example, from Connection String URI Format:


But, you can also connect to a replica-set specifying a single hostname (but, this can be a problem if that particular host is down and you are trying to connect to the replica-set).

  1. This exception is coming for a write operation will it also come for read operation

The exception can occur for read operations also. com.mongodb.MongoNotPrimaryException definition says: “An exception indicating that the server is a member of a replica set but is not the primary, and therefore refused to execute either a write operation or a read operation that required a primary. This can happen during a replica set election.”

Regarding providing all the hostnames, I do see from the logs that even though I provide just one server name, it does discover all the servers in the replicaset. So will it connect to any server required from that server name provided ? Also will it connect to the primary only as my read and write operations are to be from primary only.

Regarding MongoNotPrimary exception.
So what can we do to avoid the exception ? Is there any setting which we can provide so it will wait for the election to happen ? Also I have an instance of Mongo client which is already connected to the DB , will it internally change the connection to connect to the new primary ?

@Rajan_Shah I am facing a similar issue were you able to resolve it out. If yes could you help me also how you managed to work with it?


Were you able to resolve the error Notprimary exception ?

1 Like