Search code examples
mongodbmongodb-java

MongoDB URL without replicaSet optional parameter


I have a mongoDB cluster

   server1:27017
   server2:27017
   server3:27017

For historical reason, IT team could not provide the replicaSet name for this cluster.

My question is: without knowing the replicaSet name, is the following mongoDB url legal and will missing the optional replicaSet optional parameter cause any possible problems in future?

mongodb://username:password@server1:27017,server2:27017,server3:27017

I am using Java to setup MongoDB connection using the following

String MONGO_REPLICA_SET = "mongodb://username:password@server1:27017,server2:27017,server3:27017";
MongoClientURI mongoClientURI = new MongoClientURI(MONGODB_REPLICA_SET);
mongoClient = new MongoClient(mongoClientURI);

Solution

  • To clarify, although it may be functional to connect to the replica set it would be preferable to specify the replicaSet option.

    Depending on the MongoDB Drivers that you're using it may behaves slightly differently. For example quoting the Server Discovery and Monitoring Spec for Initial Topology Type:

    In the Java driver a single seed means Single, but a list containing one seed means Unknown, so it can transition to replica-set monitoring if the seed is discovered to be a replica set member. In contrast, PyMongo requires a non-null setName in order to begin replica-set monitoring, regardless of the number of seeds.

    There are variations, and it's best to check whether the connection can still handle topology discovery and failover.

    For historical reason, IT team could not provide the replicaSet name for this cluster.

    If you have access to the admin database, you could execute rs.status() on mongo shell to find out the name of the replica set. See also replSetGetStatus for more information.