Spring Boot and MongoDB Atlas

Naveen Kumar M N
4 min readJul 11, 2020

Being a Developer and holding a PhD in Googling (I Wish) i was low-spirited out when i couldn’t find a forum or post or for god’s sake an article to have an introduction or overview about connecting MongoDB’s Atlas with Spring boot app and then getting it to work with heroku.

Have a look at the app before jumping. Link

The Repo Link is in GitHub

Photo by Kevin Ku on Unsplash

After (coffeeCups <10) &&(days < 2) i was finally able to get it to work. Whoosh! that was quite some time.

Lets dig in then. This is a simple REST app with basic operations like adding a new quote, getting all quotes, finding quotes with specific word and getting quotes by authors. You can view it right over here.

The Project runs on QuotesModel, QuotesController and MongoRepository classes doing all the magic.

Lets look into the POJO. The document is “Quotes” with Quote and Author as fields.

To the controller method which adds a new quote, gets all quotes, finding quotes with specific word and getting quotes by authors.

Now to the DB side. Create a free account (thank god) and hola we have a cluster with 512MB to 5GB storage. Cool as a cucumber this would be abundant for the quotes.

Its time to create organisation and cluster. Create a organisation and then you would land in the organisation page. Lets name our project something like QuotesApp. Its time to spin up our cluster.

Head to create cluster page and select the cloud provider and the region. We can spin up a single cluster per project for free.

Creating a Cluster

M0 is the free cluster for backup we need M2 or higher. Pick a name and hit create cluster

We have a cluster yippee.

The connection and size are real time stats. Its time to connect our Spring Boot app. Hit on connection and we have a basic connection IP and DB user page. Fill in your connection IP with 0.0.0.0/0 cause heroku free deployment assigns dynamic IP address when deployed we open up all the incoming traffic from all IP’s. Create a DB username and password along with access controls.

Choosing Connection Type

Head to next Choose a Connection Method and click on connect your application.

Select the JAVA driver and latest version. We are provided with a connection string which we are gonna use to connect to the cluster.

Replace the username and password which we created in previous step in application.properties file.

Data Base Time

Head to your clusters collection and its time to create and load a data. Click on Add My Own Data and choose a Database name and Collection name. Voila!!!!!!.

We have a DB. Finally!!

Its Then Heroku

Create a app and give it a good name (best of luck in finding one). One of the best way is to connect github with it and

Deploy the master branch it would take up to 2–3 minutes and it would run the maven build and after successful deployment its time to open the app.

Finally, the App

Home page with swagger.

There you go, the very own quotes app with Spring Boot and Mongo DB.

--

--