Learning Objectives
- Explain what MongoDB is.
- Explain the difference between SQL and NoSQL.
- Create a MongoDB Cluster on Atlas.
- Connect your Spring Boot project with a MongoDB Cluster.
Read the following document Attention to details
Main Topics
- NoSQL.
- MongoDB.
- Cluster.
🗣️ "I hear and I forget I see and I remember I do and I understand." Confucius
If you haven't created your MongoDB Cluster follow part 1 - 4:
-
Login into your MongoDB Atlas account
-
Choose the Java driver, select the latest version and copy the connection string:
-
Replace the password on the connection string with the password used when creating your database user.
-
Add an Environment Variable to the application.properties file to store the MongoDB URI:
spring.data.mongodb.uri=${MONGODB_URI}
-
Add the environment variable to IntelliJ Idea by editing the Run/Debug Configurations:
-
Add the Spring Boot starter data MongoDB dependency to your build.gradle:
dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-data-mongodb' testImplementation 'org.springframework.boot:spring-boot-starter-test' }
-
Run your project and verify that the connection is successful.
-
Create a new package called repository.
-
Create a new class called UserDocument:
Java:
import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.index.Indexed; import org.springframework.data.mongodb.core.mapping.Document; import java.util.Date; @Document public class UserDocument { @Id String id; String name; @Indexed( unique = true ) String email; String lastName; Date createdAt; public UserDocument() { } }
Kotlin:
@Document class User( @Id var id: String?, var name: String, var lastName: String, @Indexed(unique = true) var email: String, var createdAt: Date )
-
Create a new interface called UserRepository inside the repository package:
Java:
import org.springframework.data.mongodb.repository.MongoRepository; public interface UserRepository extends MongoRepository<UserDocument, String> {}
Kotlin:
interface UserRepository: MongoRepository<UserDocument, String>
-
Create a new UserService implementation called UserServiceMongoDB and inject inside the UserRepository:
Java:
import java.util.List; public class UserServiceMongoDB implements UserService { private final UserRepository userRepository; public UserServiceMongoDB(@Autowired UserRepository userRepository ) { this.userRepository = userRepository; } @Override public User create( User user ) { return null; } @Override public User findById( String id ) { return null; } @Override public List<User> all() { return null; } @Override public boolean deleteById( String id ) { return false; } @Override public User update( UserDto userDto, String id ) { return null; } }
Kotlin:
class UserServiceMongoDB(@Autowired private val userRepository: UserRepository) : UserService { override fun create(user: User): User? { return null } override fun findById(id: String): User? { return null } override fun all(): List<User>? { return null } override fun deleteById(id: String): Boolean { return false } override fun update(userDto: UserDto, id: String): User? { return null } }
-
Implement the methods of the UserServiceMongoDB using the UserRepository.
-
Remove the @Service annotation from the UserServiceHashMap and add it to the UserServiceMongoDB.
-
Test your API and verify that your data is stored in your cluster.
-
Modify the UserService interface adding the following methods:
Java:
List<User> findUsersWithNameOrLastNameLike(String queryText); List<User> findUsersCreatedAfter(Date startDate); {}
Kotlin:
fun findUsersWithNameOrLastNameLike(queryText: String): List<User> fun findUsersCreatedAfter(startDate: Date): List<User>
Tip: take a look at the official documenation and learn how to create custom queries with Spring Data