如何在使用带有 Spring 数据的 MongoRepository 的查询注释时显示查询

我在 Spring Boot 中使用 MongoRepository 来访问 mongo:

I'm using MongoRepository in spring boot to access mongo:

public interface MongoReadRepository extends MongoRepository<User, String> {
    @Query(value = "{$where: 'this.name == ?0'}", count = true)
    public Long countName(String name);
}

它可以工作,但我想知道它访问 mongo 的确切查询

and it could work, but i wonder know the exactly query it accessing mongo

怎么做?

我尝试在如下属性中添加一些配置:

i try to adding some config at properties like below:

logging.level.org.springframework.data.mongodb.core.MongoTemplate=DEBUG
logging.level.org.springframework.data.mongodb.repository.Query=DEBUG

并且不工作.

有人可以帮忙吗?

推荐答案

我在 application.properties 中添加以下行并且工作正常:

logging.level.org.springframework.data.mongodb.core.MongoTemplate=DEBUG

查询:

@Query("{$and: [{'$or' : [{ 'name': {$regex : ?0, $options: 'i'}}, {'description': {$regex : ?1, $options: 'i'}}]}, { 'deleted' : ?2 }]}")

获取此日志:

2016-09-27 10:53:26.245 DEBUG 13604 --- [nio-9090-exec-3] o.s.data.mongodb.core.MongoTemplate      : find using query: { "$and" : [ { "$or" : [ { "name" : { "$regex" : "c" , "$options" : "i"}} , { "description" : { "$regex" : "c" , "$options" : "i"}}]} , { "deleted" : false}]} fields: null for class: class com.habber.domain.Entity in collection: entities

相关文章