DEV Community

Shreeprabha bhat
Shreeprabha bhat

Posted on

Why I Switched from H2 to MongoDB in My Growing Spring Boot App

When I started building my Expense Tracker application, all I cared about was testing my APIs without the complications of setting up a database. I just needed something lightweight that would let me test my APIs and move fast.

That’s when I chose H2 Database. It was easy, embedded, and worked right out of the box. But as my application grew and I started incorporating real life scenarios into the app, I began hitting the limitations of H2. That’s when I realized it was time to move to something more scalable and production ready MongoDB.

Honestly, my decision to use MongoDB wasn’t driven by any specific reason. It was simply pre installed on my computer, and I made the laziest move possible I went ahead and used what was already available.

I also had hands on experience testing MongoDB applications when I worked as a tester. Now, using MongoDB to develop my application after switching to development actually made my work more interesting and explorative.

Installing MongoDB Compass

MongoDB Compass is the official GUI for MongoDB that allows you to visualize, explore, and manage your data easily. Here’s how to install it on different operating systems:

  • Windows

  • Download the installer from https://www.mongodb.com/try/download/compass

  • Run the .exe file and follow the installation wizard.

  • Once installed, launch Compass and connect to your local MongoDB instance using:
    mongodb://localhost:27017

  • macOS

  • Download the .dmg file from https://www.mongodb.com/try/download/compass

  • Open the .dmg file and drag MongoDB Compass into your Applications folder.

  • Open Compass and connect using:
    mongodb://localhost:27017

  • Linux

  • Download the .deb package from https://www.mongodb.com/try/download/compass

  • Install it using the terminal:
    sudo dpkg -i mongodb-compass_<version>_amd64.deb
    sudo apt-get install -f

  • Launch Compass from the applications menu or by running:
    mongodb-compass

  • Connect to your MongoDB instance using:
    mongodb://localhost:27017

Configuring MongoDB in Your Spring Boot App

After installing MongoDB and Compass, the next step is to configure your Spring Boot application to use MongoDB instead of H2. Here’s how I did it:

  • Add MongoDB Dependency
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
            <version>4.0.0-M2</version>
        </dependency>
Enter fullscreen mode Exit fullscreen mode
  • Update application.properties In src/main/resources/application.properties, configure the connection to your local MongoDB instance:
spring.data.mongodb.uri=mongodb://localhost:27017/db_name
Enter fullscreen mode Exit fullscreen mode

Here, db_name is the name of the database MongoDB will create automatically if it doesn’t exist.

  • Update Your Entities

Since MongoDB is document-based, you need to annotate your model classes with @Document instead of JPA’s @Entity.

  • Update Repositories

Switch your repositories to extend MongoRepository instead of JpaRepository:

public interface ExpenseRepository extends MongoRepository<Expense, ObjectId>
Enter fullscreen mode Exit fullscreen mode
  • Test Your App

Run your application and add some expenses. Open MongoDB Compass and you should see your data persisted in the expenses collection. Unlike H2, your data won’t disappear when the app restarts.

Conclusion

Switching from H2 to MongoDB was a simple decision that made a huge difference in my application’s growth. Unlike H2, MongoDB is schemaless, which means I don’t have to define rigid table structures upfront. This flexibility allows me to easily evolve my Expense Tracker app as new features are added, without worrying about database migrations.

Another reason I appreciate MongoDB in my Expense Tracker app is for the Reports API. Reports often need flexible structures sometimes aggregating expenses by day, category, or user, and the fields can vary depending on the type of report. In a traditional table-based database like H2, this would require multiple joins or schema changes. With MongoDB’s document based design, I can store dynamic report data easily, making it much more flexible and scalable for real life reporting needs.

MongoDB’s document based structure also makes it great for handling dynamic data, scalable applications, and real-time analytics. Beyond small projects like mine, MongoDB is widely used in building content management systems, IoT applications, e commerce platforms, and data intensive web services.

Looking back, H2 was perfect for prototyping and testing, but for any real life application with growing data and users, MongoDB is the tool that lets your app scale, adapt, and thrive.

Top comments (0)