DEV Community

Discussion on: Insert Data in MongoDB with Spring Boot - Building a Blog

sdiamante13 profile image
Steven Diamante • Edited on

I can explain the difference between MongoTemplate and MongoRepository. So Mongo Repository is from Spring Data JPA. Out of the box you get methods like save, deleteById, findById, and findAllById. There's also a way you can write queries just by creating a specific method. So let's say you want to get a List if Blog Posts from a particular category you could define a method without any implementation called List findAllByCategory(String category). Under the hood Spring writes the appropriate query for you. It's pretty cool stuff!
MongoTemplate is a bit more lower level where you need to write your own queries. With embedded documents and denormalization it can be easier to write complex queries with MongoTemplate. For simple things I would use MongoRepository. I've seen some examples where both are used together in a hybrid approach. I hope that helps.