Furkan Yavuz
Furkan Yavuz

Reputation: 2024

How can I change/define default database of Mongodb in Spring-data?

I have interfaces extends from MongoRepository. They are using default database of mongodb. I would like to define the database name for the classes.

public interface CustomerRepository extends MongoRepository<Customer, String> {
    ...
}

How can I define it?

Upvotes: 7

Views: 11904

Answers (3)

Kris
Kris

Reputation: 1724

If you are using Spring Boot, the following steps might help you.

  • Define the following properties in application.properties or yml descriptor. note the properties should start with spring.data.mongodb. If you are using Mongo 3.x java driver, spring.data.mongodb.uri should be used to provide uri configurations.

    spring.profiles: myprofile
    spring.data.mongodb.uri: mongodb://user:passwd@url:port/dbname 
    spring.data.mongodb.database:myDB

  • Write SpringMongoConfiguration.

    @Configuration
    @EnableMongoRepostories("path.to.your.repository")
    public class SpringMongoConfiguration extends AbstractMongoConfiguration {
    
    @Value("${spring.data.mongodb.uri}")
    private String mongoDB;
    
    @Value("${spring.data.mongodb.uri}")
    private String mongoURI;
    
    @Override
    protected String getDatabaseName() {
        // TODO Auto-generated method stub
        return mongoDB;
    }
    
    @Override
    public MongoMappingContext mongoMappingContext()
            throws ClassNotFoundException {
        // TODO Auto-generated method stub
        return super.mongoMappingContext();
    }
    
    @Override
    @Bean
    public Mongo mongo() throws Exception {
        return new MongoClient(new MongoClientURI(myURI));
    }
     }
    
  • Build your project and run your spring boot app

    java -jar -Dspring.profiles.active=myprofile your-app.jar

Upvotes: 4

Sanjay Rawat
Sanjay Rawat

Reputation: 2364

You just need to define respective mongobd properties in application.properties file or if you want to yml syntax then define props in application.yml. Under src/main/resources, application.properties should be there already.

application.properties :

spring.data.mongodb.host=<hostname> 
spring.data.mongodb.port=27017 
spring.data.mongodb.database=<dbname>
spring.data.mongodb.username=<usernamr>
spring.data.mongodb.password=******

Or

application.yml :

spring: 
    data: 
        mongodb: 
            host: <hostname> 
            port: 27017 
            database: <dbname>
            username: <usernamr>
            password: ******

Upvotes: 12

Kris
Kris

Reputation: 1724

If you are using context XML add the following in your XML and define the database configurations.

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mongo="http://www.springframework.org/schema/data/mongo"
       xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <mongo:mongo-client credentials="user:password@database" />

    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongo" ref="mongo"/>
        <constructor-arg name="databaseName" value="myDBName"/>
    </bean>
</beans>

Alternatively, Define a class by extending AbstractMongoConfiguration and override the getDatabaseName().

@Configuration
public class SpringDBConfig extends AbstractMongoConfiguration
{ 

    @Override
    protected String getDatabaseName() {
        return "testdatabase";
    }
    ......         
}

Upvotes: -1

Related Questions