java – Spring Boot + MongoDB project structure and database creation

I’m planning to start a new Spring Boot project with MongoDB. I’m very familiar with Spring Boot, but all of my past projects used MySQL.

Each of my projects has the following directory, that holds the SQL creation/migration scripts:

│   ├── db-scripts
│   │   └── my-db
│   │       └── schema
│   │           ├── V202101250000__create_....sql
│   │           ├── V202101250001__create_....sql
│   │           ├── V202101250002__update_....sql
│   │           ├── ...

and I used Flyway to manage the migration of the above scripts.

To summarize the way I’m currently working:

  • I create the DBs and tables before the code run, so when it runs, it has all DB-related stuff up
  • Each table is mapped by a corresponding Java entity (Spring’s JPA @Entity)
  • Before each upgrade, I run Flyway migrations as part of the release

Now, my questions regarding working with MongoDB:

  • Should I create the databases beforehand, or let Spring create them upon the first insert?
    • If so, should I have manual scripts for this (e.g. db.createCollection(), …)?
  • Should I use a migration tool (e.g. mongobee), or it’s “less common” approach when working with Mongo?
  • Should each document have a corresponding Spring entity, the same way I had for SQL when it comes to validation?