How to use multiple mongodb databases in spring boot application?

pavan kalyan picture pavan kalyan · Sep 19, 2018 · Viewed 11.8k times · Source

In my application, I need to use two MongoDB databases. I don't know how to add 2 MongoDB databases in the application.properties file in the spring application.

Here is the application.properties file of my project,

spring.data.mongodb.database=DB1
spring.data.mongodb.authentication-database=DB1
spring.data.mongodb.host=dev-ng-mongo1.domain.com
spring.data.mongodb.password=9876512
spring.data.mongodb.port=27017 
spring.data.mongodb.username=pavan

but I want to use another MongoDB database for the same project. How can I add the new database in the application.properties file.

Answer

kj007 picture kj007 · Sep 19, 2018

Please follow below steps to setup multiple mongodb data sources.

  1. Define your primary and secondary mongodb properties like below in application.properties, please replace with them your db details:

    ######Primary Mongo DB########################
    spring.data.mongodb.host=localhost
    spring.data.mongodb.database=primary
    spring.data.mongodb.port=27017
    spring.data.mongodb.password=*******
    spring.data.mongodb.username=*******
    
    ###########Secondary MongoDB#####################
    mongodb.host=localhost
    mongodb.port=27017
    mongodb.database=secondary
    mongodb.username=******
    mongodb.password=******
    
  2. Now add Multiple Mongo Db Configuration..

    @Configuration
    public class MultipleMongoConfig {
    
    @Primary
    @Bean(name = "primary")
    @ConfigurationProperties(prefix = "spring.data.mongodb")
    public MongoProperties getPrimary() {
        return new MongoProperties();
    }
    
    @Bean(name = "secondary")
    @ConfigurationProperties(prefix = "mongodb")
    public MongoProperties getSecondary() {
        return new MongoProperties();
    }
    
    @Primary
    @Bean(name = "primaryMongoTemplate")
    public MongoTemplate primaryMongoTemplate() throws Exception {
        return new MongoTemplate(primaryFactory(getPrimary()));
    }
    
    @Bean(name = "secondaryMongoTemplate")
    public MongoTemplate secondaryMongoTemplate() throws Exception {
        return new MongoTemplate(secondaryFactory(getSecondary()));
    }
    
    @Bean
    @Primary
    public MongoDbFactory primaryFactory(final MongoProperties mongo) throws Exception {
        return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()),
                mongo.getDatabase());
    }
    
    @Bean
    public MongoDbFactory secondaryFactory(final MongoProperties mongo) throws Exception {
        return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()),
                mongo.getDatabase());
    }
    

    }

  3. Now enable EnableMongoRepositories for your primary an secondary.please make sure you change basePackages = "com.example.springbootmultipledatasource.primary.repository" your repository package here

    @Configuration
    @EnableMongoRepositories(basePackages = 
    "com.example.springbootmultipledatasource.primary.repository",
        mongoTemplateRef = "primaryMongoTemplate")
     public class PrimaryMongoConfig {
    
     }
    

Secondary Mongo Template:Please make sure you change your secondary repository package here basePackages = "com.example.springbootmultipledatasource.secondary.repository

    @Configuration
    @EnableMongoRepositories(basePackages = "com.example.springbootmultipledatasource.secondary.repository",
        mongoTemplateRef = "secondaryMongoTemplate")
   public class SecondaryMongoConfig {
   }

Now you can create your document, repository, service, controller and you are good to go.below is my project structure you can create or have different.

Project Structure