Overview:

In this tutorial we will be learning how to connect your Spring Boot application with a MongoDB instance that is hosted using MongoDB Atlas. Atlas has a free “sandbox” that can be configured – this is what we will use for this tutorial.

This tutorial assumes that you already have a Spring Boot project and Atlas account.

Requirements:

Getting MongoDB Connection Details

At this time, sign in to Atlas and access your main dashboard. This dashboard is where you can configure your first database and collection if you haven’t already done this.

Select “Sign In” option in the upper-right corner

Once you are signed in and have access to your cluster, you must be sure that a user exists which has permission to access your database and collection. To configure a new database user, select the “Database Access” menu item from the left. Now, press “Add New User” and follow the prompts to setup a new user with your desired permissions. Once this is completed you will see the user appear in a table of all configured users.

Create new User

Finally! Now we are ready to get our connection string for our database! Luckily, Atlas takes away all the guess work and provides a connection string or code sample – I know I’ve had issues keeping my connection string criteria organized in the past.

Press “Connect” on the sandbox cluster you’ve created. Then, select the connection method; in this case we want to choose “Connect Your Application” since we will be connecting with our Spring Boot app.

Choose Connection Method

Next, you are able to tell Atlas which programming language and version of the MongoDB driver you are using. In our case, we will select “Java” as the language and “3.6+” as the version. If you’re using a new DB and Spring Boot project, this will be the same for you.

After selecting the language and version, Atlas will provide the connection string. NOTICE the <username> and <password> placeholders, this is where you will configure the username and password of the user you just created.

Copy the connection string.

Adding the Connection from Spring Boot

Now it’s finally time to open your Spring Boot project, in this tutorial I’m using Eclipse.

Once your application is open, make sure you have the appropriate dependencies in your POM or Gradle file. The following is an example of the Reactive MongoDB Maven dependency.

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>

Open your “application.properties” file or create one in the “src/main/resources” directory.

Insert your connection string with the property “spring.data.mongodb.uri”, be sure to replace to placeholder values with your actual username and password. 

Next, configure “spring.data.mongodb.database” with the name of the database you’d like to connect with.

Add Connection String

Verify that the connection was successful

The very last thing to do is test the connection. Simply start your Spring Boot application and watch the output in your IDE to verify that the connection was made. If it was successful, you will see a log similar to the following.

verify startup logs

That’s it! Congratulations on making through this tutorial! Please take a moment to comment if you have any questions or issues.

If you’re a beginner with MongoDB and are looking for a great reference; I’ve personally found that Mastering MongoDB (amazon affiliate link) was a good choice, as it’s been recently updated for MongoDB 4.x.