Search code examples
springmongodbspring-dataspring-boot

Spring Boot and how to configure connection details to MongoDB?


Being new to Spring Boot I am wondering on how I can configure connection details for MongoDB. I have tried the normal examples but none covers the connection details.

I want to specify the database that is going to be used and the url/port of the host that runs MongoDB.

Any hints or tips?


Solution

  • Just to quote Boot Docs:

    You can set spring.data.mongodb.uri property to change the url, or alternatively specify a host/port. For example, you might declare the following in your application.properties:

    spring.data.mongodb.host=mongoserver
    spring.data.mongodb.port=27017
    

    All available options for spring.data.mongodb prefix are fields of MongoProperties:

    private String host;
    
    private int port = DBPort.PORT;
    
    private String uri = "mongodb://localhost/test";
    
    private String database;
    
    private String gridFsDatabase;
    
    private String username;
    
    private char[] password;