Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 994
Furkan Yavuz Avatar asked May 14 '16 11:05

Furkan Yavuz


1 Answers

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: ******
like image 133
Sanjay Rawat Avatar answered Oct 04 '22 03:10

Sanjay Rawat