How to integrate MongoDB with the Spring Boot application
In today’s growing world with increasing data applications and parameters for a single entity, which leads to a non-sql database. So in a few minutes, we are gonna integrate MongoDB with our Spring boot application.
Example Integration with MongoDB
Step 1: Add Dependencies
First, you need to add the required dependency in pom.xml
. You need to add the MongoDB library which will help us to connect to the MongoDB server.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
Step 2: Create a POJO class
Create a POJO class name student which will reflect as a collection in our database and we will gonna use @Document(collection = ”students”)
annotation which will represent a collection name in the database.
@Document(collection = "students")
public class Student {
private int id;
private String name;
private String city;
private String college;
public Student(String college, String city, String name, int id) {
this.college = college;
this.city = city;
this.name = name;
this.id = id;
}
public int getId() {
return id;
}
public String getCollege() {
return college;
}
public String getCity() {
return city;
}
public String getName() {
return name;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setCity(String city) {
this.city = city;
}
public void setCollege(String college) {
this.college = college;
}
}
Step 3: Create a Repository Interface
We need to create one Repository interface that will help us to do operations on the MongoDB collection. This interface will extend MongoRepository<Student, Integer>
which helps us to map the student data with the integer key.
public interface StudentRepo extends MongoRepository<Student, Integer> {
}
Step 4: Add a Controller class for Rest Operations
Next, we will gonna add a controller class for doing some REST operations which will help us to add and view data in the collection.
@RestController
public class MyController {
@Autowired
private StudentRepo studentRepo;
@PostMapping("/student")
public ResponseEntity<?> addStudent(@RequestBody Student student) {
Student save = this.studentRepo.save(student);
return ResponseEntity.ok(save);
}
@GetMapping("/")
public ResponseEntity<?> getStudents() {
return ResponseEntity.ok(this.studentRepo.findAll());
}
@GetMapping("/{id}")
public ResponseEntity<?> getStudentsById(@PathVariable int id) {
return ResponseEntity.ok(this.studentRepo.findById(id));
}
}
The above code helps us to add the following operations:
- @PostMapping(“/student”): This will help us to add new entry in our collection.
- @GetMapping(“/”): This will help us to get all the data that is stored in the given collection.
- @GetMapping(“/{id}”): This will get data based on the given id.
Step 5: Configuring MongoDB
Next, we’ll gonna add MongoDB port, host, and database in application.properties
file.
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=demo
Conclusion
MongoDB integration is easy and helps us to do a lot of operations without writing actual statements, unlike SQL databases.