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

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


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.


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.


Change application.properties from this:

spring.data.mongodb.host=server1spring.data.mongodb.port=27017spring.data.mongodb.authentication-database=systemspring.data.mongodb.database=database

...to this:

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