Spring Boot MongoDB+MLAB

Spring Boot MongoDB and MLAB:- 

This article demostrates how to use Spring Boot MongoDB and MLAB.The Whole advantage of using Spring boot is to auto configuration.Spring boot helps us to save a lot of time during our development.We are going to write simple application to do some basic operations using Spring Boot MongoDB.We are going to use cloud hosted mongo service mlab.This is the one of the best and free  database platform as a service for using mongo db  .



 

Configure MongDb on MLAB:-

Step 1:-Vist the website mlab and signup , once you are successfully signed up you will see the following screen.

Spring Boot MongoDB +MLAB

Step2:- Click on Create button .On the next screen you need to Choose the free plan and move to next screen .Enter database name as name of your choice.

Spring Boot MongoDB +MLAB

Step 3 :- Click on the row above you will get your mongo database details.

Spring Boot MongoDB +MLAB

Step 4:- Now lets create a mongo user.We can’t leave our database unauthenticated.Click On Add Database User and add user details and hit Create. Store the user and password at some safe place.

Spring Boot MongoDB +MLAB

So now We have got our mongodb URL, replace username and password with the one you created earlier:-

Create A Spring Boot MongoDB Application :-

I am Covering the Spring Boot Mongo DB in short, as i am planning to cover REST Services using Spring BootMongoDB as well.There I will explain each and every component in a better way .

Project Structure:-

Use https://start.spring.io/ to create a Spring boot MongoDB Project .

Project Dependencies:

You can use start.spring.io to create a project with dependency as MongoDB.

pom.xml:-

Configure Application Properties File:-

application.properties:-

Create a User Database Bean To be Stored In MongoDB :-

User.java:-

In Spring Boot MongoDB,If we dont specify collection name it will take classname as collection name by default.

Create Repository To Access Collections in Spring Boot MongoDB :-

There is no concept of table when it comes to mongodb or any no sql database.Lets create a simple repository that access data from mongodb .

Save and Retrieve Data from Spring Boot Mongo DB:-

MainApplication.java

Output:- 

Run the command mvn spring-boot:run

MLab Output:-

Spring Boot MongoDB +MLAB







 

2 thoughts on “Spring Boot MongoDB+MLAB

Leave a Reply

Your email address will not be published. Required fields are marked *