Difference between spring boot mongoTemplate and mongoRepository.
Both Spring MongoTemplate and MongoRepository are part of the Spring Data MongoDB framework and provide different approaches for interacting with MongoDB in a Spring Boot application.
Spring MongoTemplate:
Spring MongoTemplate is a general-purpose MongoDB API provided by Spring Data MongoDB. With MongoTemplate, you have full control over the MongoDB operations and can execute complex queries, perform updates, and handle the mapping between Java objects and MongoDB documents manually.
Example:
@Repository
public class UserRepository {
@Autowired
private MongoTemplate mongoTemplate;
public User saveUser(User user) {
mongoTemplate.save(user);
return user;
}
public User findUserById(String id) {
Query query = new Query(Criteria.where("_id").is(id));
return mongoTemplate.findOne(query, User.class);
}
public List<User> findAllUsers() {
return mongoTemplate.findAll(User.class);
}
// Custom query example
public List<User> findUsersByAgeGreaterThan(int age) {
Query query = new Query(Criteria.where("age").gt(age));
return mongoTemplate.find(query, User.class);
}
public void deleteUser(User user) {
mongoTemplate.remove(user);
}
}
In the above example, the UserRepository class uses Spring MongoTemplate to interact with MongoDB. It includes methods to save a user, find a user by ID, retrieve all users, perform a custom query to find users by age, and delete a user.
MongoRepository:
MongoRepository, on the other hand, is a higher-level abstraction provided by Spring Data MongoDB. It follows the repository pattern and provides a simplified and declarative approach for CRUD (Create, Read, Update, Delete) operations with MongoDB. MongoRepository offers a set of predefined methods such as save, findById, findAll, delete, etc., which you can use out of the box without writing explicit queries or mapping code.
Example:
@Repository
public interface UserRepository extends MongoRepository<User, String> {
List<User> findByAgeGreaterThan(int age);
// Additional custom query methods can be defined
void deleteByFirstName(String firstName);
}
In this example, the UserRepository interface extends the MongoRepository interface provided by Spring Data MongoDB. It includes methods such as save, findById, findAll, and delete, which are inherited from MongoRepository. Additionally, it defines a custom query method findByAgeGreaterThan
to find users by age and a custom delete method deleteByFirstName
to delete users by their first name.
With MongoRepository, you don’t need to provide implementation code for these methods. Spring Data MongoDB automatically generates the necessary implementation based on the method names and conventions.
To use the UserRepository in your service or controller, you can autowire it and invoke the inherited methods:
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public User saveUser(User user) {
return userRepository.save(user);
}
public User findUserById(String id) {
return userRepository.findById(id).orElse(null);
}
public List<User> findAllUsers() {
return userRepository.findAll();
}
public List<User> findUsersByAgeGreaterThan(int age) {
return userRepository.findByAgeGreaterThan(age);
}
public void deleteUser(User user) {
userRepository.delete(user);
}
}
In the above UserService class, the UserRepository methods are used to perform CRUD operations on users without writing explicit queries or mapping code.
Summary:
Spring MongoTemplate offers more control and flexibility with manual query and mapping code, while MongoRepository provides a higher-level abstraction with pre-defined methods for common CRUD operations and automatic mapping.
Happy Coding……..