In this tutorial, we will discuss how to connect two different MongoDB databases from single Spring boot project.
To connect to multiple MongoDB databases in Spring boot, you can use the MongoClientFactoryBean
and create multiple MongoClient
instances.
At first we will need to define MongoProperties
bean for each database you want to connect to. For example:
@Configuration
public class MongoConfig {
@Bean
@ConfigurationProperties(prefix = "spring.data.mongodb.primary")
public MongoProperties firstMongoProperties() {
return new MongoProperties();
}
@Bean
@ConfigurationProperties(prefix = "spring.data.mongodb.secondady")
public MongoProperties secondMongoProperties() {
return new MongoProperties();
}
// ...
}
In the above example, we're defining two MongoProperties
beans, one for each database. The @ConfigurationProperties
annotation is used to bind the properties from the application.properties
file to the corresponding fields in the MongoProperties
class.
Here is how our properties defined in application.properties
file
# Primary MongoDB configs
spring.data.mongodb.primary.host=yourmongodb1.host.url
spring.data.mongodb.primary.port=27017
spring.data.mongodb.primary.username=YOUR_DB_USERNAME
spring.data.mongodb.primary.password=YOUR_DB_PASSWORD
spring.data.mongodb.primary.database=DATABASE_NAME
# Secondary MongoDB configs
spring.data.mongodb.secondary.host=yourmongodb2.host.url
spring.data.mongodb.secondary.port=27017
spring.data.mongodb.secondary.username=YOUR_DB2_USERNAME
spring.data.mongodb.secondary.password=YOUR_DB2_PASSWORD
spring.data.mongodb.secondary.database=DATABASE2_NAME
Next, we'll define a MongoClientFactoryBean
for each database. For example:
@Configuration
public class MongoConfig {
// ...
@Bean
public MongoClientFactoryBean firstMongoClientFactoryBean(MongoProperties mongoProperties) {
MongoClientFactoryBean factoryBean = new MongoClientFactoryBean();
factoryBean.setHost(mongoProperties.getHost());
factoryBean.setPort(mongoProperties.getPort());
factoryBean.setCredentials(new MongoCredential[]{
MongoCredential.createCredential(
mongoProperties.getUsername(),
mongoProperties.getDatabase(),
mongoProperties.getPassword().toCharArray())
});
return factoryBean;
}
@Bean
public MongoClientFactoryBean secondMongoClientFactoryBean(MongoProperties mongoProperties) {
MongoClientFactoryBean factoryBean = new MongoClientFactoryBean();
factoryBean.setHost(mongoProperties.getHost());
factoryBean.setPort(mongoProperties.getPort());
factoryBean.setCredentials(new MongoCredential[]{
MongoCredential.createCredential(
mongoProperties.getUsername(),
mongoProperties.getDatabase(),
mongoProperties.getPassword().toCharArray())
});
return factoryBean;
}
// ...
}
In the above example, we're defining two MongoClientFactoryBean
beans, one for each database. We're setting the host, port, and credentials for each factory bean using the corresponding properties from the MongoProperties
beans.
Finally, we'll define MongoTemplate
beans for each database, using the MongoClient
instances created by the MongoClientFactoryBean
beans. For example:
@Configuration
public class MongoConfig {
// ...
@Bean
public MongoTemplate firstMongoTemplate(MongoClient firstMongoClient, MongoProperties mongoProperties) {
return new MongoTemplate(firstMongoClient, mongoProperties.getDatabase());
}
@Bean
public MongoTemplate secondMongoTemplate(MongoClient secondMongoClient, MongoProperties mongoProperties) {
return new MongoTemplate(secondMongoClient, mongoProperties.getDatabase());
}
// ...
}
In the above example, we're defining two MongoTemplate
beans, one for each database. We're passing in the MongoClient
instances created by the MongoClientFactoryBean
beans, along with the corresponding database name from the MongoProperties
beans.
That's it! With the above configuration, you should be able to connect to multiple MongoDB databases in Spring Boot.