I have a replica of three mongod instances, one of them is the primary instance, and the other two are secondary. I connected a mongo instance to the primary, and then I turned off the mongod primary instance, simulating a failure.
Typically, as soon as I initiated a command, mongo generated a DBClientCursor::init call() failed error.
My question is, in case of such a failure, where the primary cannot go back to service, and there will definitely be an election for the new primary, is there a way to tell mongo to automatically search and connect to the new primary of the replica set?
Possibly your problem is that you're only connecting to one node, the one which is primary at that time. What you need to to do is to specify, when connecting, what the replicaset is and what the address is of each available node - so your client can reconnect to a different node if its primary falls over.
The documentation on the Mongo URI connection string format explains this:
It gives this example:
You may need to check your driver's documentation also, in case it requires a particular way of specifying the replica set and the list of nodes.