Ruth Shaves
Ruth Shaves

Reputation: 154

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?

Upvotes: 7

Views: 35274

Answers (5)

Pedro Lopez
Pedro Lopez

Reputation: 1

We used:

server:
  port: 8090
spring:
  application:
    name: {yourappname}
  data:
    mongodb:
      host: localhost
      port: 27017
      database: coins
      auto-index-creation: true

Upvotes: 0

fayssal el ansari
fayssal el ansari

Reputation: 105

you can put the same link you use to connect in mongoDbCompass in the URI property, make sure to let a space after each properties, for me I forgot to add a white space after uri: the working yml snippet is as follows:

spring:
  data:
    mongodb:
      uri: mongodb+srv://<username>:<password>@cluster0.tt44qmp.mongodb.net/test

Upvotes: 0

Siddharth Aadarsh
Siddharth Aadarsh

Reputation: 319

spring:
  data:
    mongodb:
      authentication-database: admin
      username: #your_root_user     (default->rootuser)
      password: #your_root_password (default->rootpass)
      database: #your_db_name
      port: #your_port              (default->27017)
      host: #your_host              (default->localhost)

For me this worked.

Upvotes: 2

yuen26
yuen26

Reputation: 1038

You can do as below:

spring:
  data:
    mongodb:
      uri: mongodb://yourusername:yourpassword@localhost:27017/yourDB

Upvotes: 3

Rob Scully
Rob Scully

Reputation: 790

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

Upvotes: 18

Related Questions