MongoDB Connection String to Replica Set
The multiple servers in the connection string serve as a seed list for discovering the connection mode. You are correct in that you could just specify the primary server and things would work perfectly. That is, until the primary server goes down or is very busy. By specifying multiple machines in the connection string, you give the client more than one location to query for the replica set configuration.
When the connection mode resolves to a replica set (see more below), the driver will find the primary server even if it is not in the seed list, as long as at least one of the servers in the seed list responds (the response will contain the full replica set and the name of the current primary). In addition, other secondaries will also be discovered and added (or removed) into the mix automatically, even after initial connection. This will enable you to add and remove servers from the replica set and the driver will handle the changes automatically.
To answer your final question, because specifying multiple servers is ambiguous as to whether or not it is a replica set or multiple mongos (in a sharded setup), the driver will go through a discovery phase of connecting to the servers to determine their type. This has a little overhead at connection time and can be avoided by specifying a connection mode in the connection string - hence the replicaSet
keyword. So while it is not necessary, it can speed up your connection times to explicitly state the servers are in a replica set in the connection string.
Since MongoDB 3.6 one can use DNS Seedlist Connection Format.
For example,mongodb+srv://server.example.com/
might turn into to the following connection string mongodb://mongodb1.example.com:27317,mongodb2.example.com:27017/?replicaSet=mySet&authSource=authDB
.
Once your records are set up, you can easily change port numbers without impacting clients and also add and remove cluster members