Let me start by showing you the configuration. I have below replica set configured on my machine.
Setup: My replica set is having 3 nodes:
If you specify single or multiple hosts (without replica set name) in the connection string, it will always connect to the first mentioned node regardless of the node being primary or secondary.
Please check below screenshots:
However if you specify replica set name along with the host, then you will always be connected to the primary node of the replica set:
To check the node to which you are connected you can do so using db.serverStatus().
In this case, if you mention the replica set name, then you will connect to the primary node of the replica set. It will also show the error message while connecting to the non-existent node as shown in the screenshot below:
If you skip the replica set name, then it will connect to the other node mentioned in the connection string. In this case also, error message will be displayed while connecting to the non-existent node:
I hope it helps!!
Please let me know, if you have any questions.