How to configure spring-data-mongodb to use a replica set via properties

Change application.properties from this:

spring.data.mongodb.host=server1
spring.data.mongodb.port=27017
spring.data.mongodb.authentication-database=system
spring.data.mongodb.database=database

...to this:

spring.data.mongodb.uri=mongodb://username:password@server1:port,server2:port/database

I had a similar problem and I dug into the MongoProperties::createMongoClient() code and found that the code was ignoring the uri value if there were any values configured for spring.data.mongodb.host, spring.data.mongodb.port, spring.data.mongodb.username or spring.data.mongodb.password.

If I put all that information in the URI (and removed all the other spring.data.mongodb.* values from the property file), the connection code worked.

The URI property setting ended up looking like this:

mongodb://username:mypasswd@hostname1:27017,hostname2:27017,hostname3:27017/dbname

The docs for formatting your URI value are here.


There is no explicit support for that, no. But you should be able to configure that just fine via the uri parameter.

We've actually updated the documentation recently.