Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use multiple mongodb databases in spring boot application? [duplicate]

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.

like image 506
pavan kalyan Avatar asked Sep 19 '18 04:09

pavan kalyan


People also ask

Can we configure 2 DB in spring boot?

Multiple Databases in Spring BootSpring Boot can simplify the configuration above. Now we have defined the data source properties inside persistence-multiple-db-boot. properties according to the Boot autoconfiguration convention.

Can MongoDB have multiple databases?

MongoDB meets the multi-database challenge An open-source NoSQL database, MongoDB provides an elastic data model that enables users to store and query multivariate data types with ease.

What is the difference between MongoOperations and MongoTemplate?

MongoTemplate provides a simple way for you to save, update, and delete your domain objects and map those objects to documents stored in MongoDB. You can save, update and delete the object as shown below. MongoOperations is the interface that MongoTemplate implements.


1 Answers

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

like image 56
kj007 Avatar answered Nov 15 '22 23:11

kj007