How to Switch to primary node

If my login have connected to secondary node, how to switch back to primary node to do write operation.

Is this Class cluster or your Sandbox cluster?

Please use replicaset/host in your connect string so that it automatically connects to primary

mongo “mongodb://cluster0-shard-00-00-jxeqq.mongodb.net:27017,cluster0-shard-00-01-jxeqq.mongodb.net:27017,cluster0-shard-00-02-jxeqq.mongodb.net:27017/test?replicaSet=Cluster0-shard-0” --authenticationDatabase admin --ssl --username m001-student --password m001-mongodb-basics

mongo --host “m103-example/m103.mongodb.university:27011” -u “m103-admin” -p
“m103-pass” --authenticationDatabase “admin”

To add to what @Ramachandra_37567 said, including the name of the replica set + two or more nodes will cause an automatic redirect to the primary:

I’ve highlighted “two or more nodes” because that’s as equally important as including the name of the replicaset. It’s recommended to include all the data bearing nodes.

Two different ways of connecting (the second is the URI format):
mongo db_name --host "replica_set_name/node_1:port,node_2:port" --authenticationDatabase admin

mongo "mongodb://node_1:port,node_2:port/db_name?replicaSet=replica_set_name" --authenticationDatabase admin

Hi Ramachandra,

thanks for your reply, consider scenario in cluster. We have problem in connecting to primary, and mongo db connects to secondary. Is that still i can do write operations, as primary node is the only can do doing write. Will the secondary node will switch to primary ?

No.You cannot do write operations if primary is down
If primary goes down election takes place and one of the secondary promotes to primary depending on priority
You will come across this in cluster administration course

Thanks a lot bro… for clearing my doubt.