All you have to do is use -host and provide it with one of your hosts in replication, but with the replication name as the prefix.
For example:
mongo --host my_mongo_server1
will connect to my_mongo_server1, it could just be another SECONDARY node.
But:
mongo --host my_repl_set_name/my_mongo_server1
will always connect to the PRIMARY node in the replica set, even if it is not my_mongo_server1.
Why? The answer is "Replica set monitor". In the above example, the mongo shell will connect to the specified node, start a new replica set monitor for the replica set, and will use the specified node only for its seed. From there, the monitor will detect all nodes in the replica set and switch the connection to the PRIMARY node.
Hope this helped.
Doron Levari Sep 15 '15 at 1:31 2015-09-15 01:31
source share