MongoDB With Spring Boot: A Simple CRUD
In this blog, we are going to explore MongoDB with Java Spring Boot. We will create a simple CRUD API to interact with our Mongo database.
Join the DZone community and get the full member experience.Join For Free
MongoDB is an open-source non relational, document oriented database. MongoDB being document oriented means that it stores data in JSON like documents which makes it more powerful and expressive. MongoDB’s ability to scale up and down very easily is considered to be one of its advantages over its competitors. Data is stored in documents in key pair values. Another component of MongoDB is collection, which is the simple collection of documents. Collection corresponds to Table in relational databases. In this blog we are going to explore this database with Java Spring Boot. We will create a simple CRUD API to interact with our Mongo database.
Why Use MongoDB?
- It is document based and therefore it is more flexible where each document can have varying fields which can not be done in relational databases.
- It allows us to index any field in the document to improve search results.
- It provides us with rich and powerful query language which allows us to filter and sort using any field no matter how nested the field is.
- It provides us with high scalability (sharding) and high availability (replication) of data.
MongoDB With Java Spring Boot
Assuming that you have a basic understanding of MongoDB now we will now see how we can leverage MongoDB by building a small spring boot API to perform basic CRUD operations.
- You should have MongoDB installed in your local environment. To save yourself some time setting it up you can also use a MongoDB Docker image, you can see how to do it here. This application will run in the default mongo port.
- Create a Spring Boot application with web-started and mongo dependencies. You can match your pom.xml with the one provided on the project repository.
This package will have the java object model for the document. We have created a simple Person model here with fields
@Document annotation is used to define the name of the collection in which your document will be saved.
DAO in our project is the data access object. It contains the implementation for the Mongo repository.
Here, we have created an Interface
PersonRepository which extends the
MongoRepository comes with basic CRUD operations for us to use out of the box. Making our task easier.
The implementation of the
PersonRepository is in the
PersonDao class. All these operations will be done using API.
Inserting Data (Create)
insert() method will take Person object as parameter and insert the person details into MongoDB.
Getting Data (Read)
Here, we have defined two methods for reading data from MongoDB.
- For getting all person information:
It will return a collection of
- For getting information of a specific person:
This method will take
id as a parameter and return the person information matching the ID.
Updating Existing Data
updatePersonUsingId() method will take the id and the person object as parameters.
Here, the method
deletePersonUsingId() will take an
id as a parameter and delete the person data corresponding to the ID.
This is it. All these operations can be done by using the API we have given in the controller. Test this application using the Postman.
This is just a basic CRUD example for a quick walk through but production level code is more crisp, detailed and contains many scenarios. This example is no where near that.
The link for this project is given below and contains a docker-compose.yml with MongoDB image to help you run this example quickly!
Link to Github repository for the project is here.
Opinions expressed by DZone contributors are their own.