11

I am new to Spring Boot and MongoDb. Trying some examples with Mongo Repositories and Spring Boot. But after going through some of the documents found that Mongo Template is will be a better option. Unable to get a proper Spring Boot with Mongo Template example.

  1. Can someone please help me out with an example for the same.

  2. Do we need to create a User defined Repositories interface and extend Repositories or CRUD Repository, while trying for Mongo Template ?

umesh
  • 312
  • 3
  • 4
  • 15
  • 1
    did you find any solution to this? can we use both template and repositories together in same project ? lets say if I want to implement a custom repository can I use template to do so ? – Gauranga Jul 16 '17 at 16:58

2 Answers2

7

For further explanation, you can even use both at the same time.

MongoRepository is just an abstraction layer, like MongoTemplate, but with simpler interface.

If you found doing some kind of operation is too complicated with Spring query-creation, and somehow doesn't want to use @Query (for example, you want IDE type hint when constructing queries), you can extend the MongoRepository and use MongoTemplate as the query mechanism.

First we extend our repository with our custom interface.

@Repository
public interface ArticleRepository extends MongoRepository<Article, String>, CustomArticleRepository {
}

Then declare the interface.

public interface CustomArticleRepository {
    List<Article> getArticleFilteredByPage(int page, int num);
}

And then implement our custom repository. We can autowire the MongoTemplate here and use it to query the database.

public class CustomArticleRepositoryImpl implements CustomArticleRepository {

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public List<Article> getArticleFilteredByPage(int page, int num) {
        return mongoTemplate.findAll(Article.class)
                .skip(page * num)
                .take(num);
    }
}

Last, we use the ArticleRepository.

@Service
public class ArticleServiceImpl {

    @Autowired
    private ArticleRepository articleRepository;

    public List<Article> getArticleByPage() {
        return articleRepository.getArticleFilteredByPage(1, 10);
    }
}
4

I have found some examples using Mongo Template

http://docs.spring.io/spring-data/data-document/docs/current/reference/html/#mongo-template

http://www.mkyong.com/mongodb/spring-data-mongodb-hello-world-example/

If you are interested in using JPA, please see below

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>{mongo.driver.version}</version>
</dependency>

application.properties

#Mongo DB
spring.data.mongodb.database=
spring.data.mongodb.host=
spring.data.mongodb.password=
spring.data.mongodb.port=
spring.data.mongodb.repositories.enabled=
spring.data.mongodb.uri=
spring.data.mongodb.username=

SpringBoot class

@SpringBootApplication
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class })
public class UserApp {

Mongo Repository

@Repository
public interface UserRepository extends MongoRepository<User, Long> {}
Saravana
  • 12,647
  • 2
  • 39
  • 57
  • Hi Sarvanna, My question is do we need to create a Repository class when we are working with Mongo Template? As far as I read Mongo Template and Mongo Repository are two different entities. – umesh Jul 11 '16 at 08:36
  • You don't need to create both, template will be basically a wrapper over connection factory provide basic CRUD operations – Saravana Jul 11 '16 at 09:29
  • The links I have posted is for using template but if you want to use JPA you can see the code I posted – Saravana Jul 11 '16 at 09:30