Skip to content
Advertisement

Spring boot Mongo DB .yml configuration

When I use MySQL and hibernate for spring boot, I use below configuration in .yml file

spring:
  datasource:
    url: jdbc:mysql://localhost/userName?zeroDateTimeBehavior=convertToNull
    username: userName
    password: password
    driverClassName: com.mysql.jdbc.Driver

  jpa:
    show-sql: false
    hibernate:
      dialect: org.hibernate.dialect.MySQLDialect
      format_sql: false
      ddl-auto: update 

If it is mongoDB instead of MySQL and hibernate how does it change?

Advertisement

Answer

The mongodb properties are all prefixed with spring.data.mongodb. For user property you would use

spring:
  data:
      mongodb:
        user: test
        password: passwordvalue
        uri: mongodb://host:27017/db

The list of available mongodb properties are here:

https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

You can find them at source for how they are loaded on github:

https://github.com/spring-projects/spring-boot/blob/v2.1.5.RELEASE/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement