7

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?

2
  • What do you mean by database name for classes? Do you mean to say Document name (table name) for classes? Commented May 14, 2016 at 18:27
  • 1
    @SanjayRawat lets say in my mongod i have more than one database. I would like use first one. Commented May 15, 2016 at 4:54

3 Answers 3

12

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: ******
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. Can we use multiple databases?
I guess so. Found this smasue.github.io/spring-yml-datasources for JPA, can be applied to Mongo with manual configuration.
@Furkan have you found a way to use multiple connections with the default configuration?
5

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

Comments

-1

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";
    }
    ......         
}

2 Comments

There is no XML in Spring Boot.
The question has a spring-boot tag, so you can't suggest to configure a XML!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.