Java 如何在使用带有 Spring 数据的 MongoRepository 的查询注释时显示查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37118047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to show query while using query annotations with MongoRepository with spring data
提问by genchilu
I'm using MongoRepository in spring boot to access mongo:
我在 Spring Boot 中使用 MongoRepository 来访问 mongo:
public interface MongoReadRepository extends MongoRepository<User, String> {
@Query(value = "{$where: 'this.name == ?0'}", count = true)
public Long countName(String name);
}
and it could work, but i wonder know the exactly query it accessing mongo
它可以工作,但我想知道它访问 mongo 的确切查询
how to do that?
怎么做?
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
and don't work.
并且不工作。
could somebody help?
有人可以帮忙吗?
回答by Luis Costa
I add the line (below) in application.properties and works fine:
我在 application.properties 中添加了行(下面)并且工作正常:
logging.level.org.springframework.data.mongodb.core.MongoTemplate=DEBUG
for query:
查询:
@Query("{$and: [{'$or' : [{ 'name': {$regex : ?0, $options: 'i'}}, {'description': {$regex : ?1, $options: 'i'}}]}, { 'deleted' : ?2 }]}")
obtain this log:
获取此日志:
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
回答by Wilder Valera
For ReactiveMongo add this property to your .properties file
对于 ReactiveMongo,将此属性添加到您的 .properties 文件中
logging.level.org.springframework.data.mongodb.core.ReactiveMongoTemplate=DEBUG
回答by Chaojun Zhong
Also, you can use a yml config file, put it in your application.yml file.
此外,您可以使用 yml 配置文件,将其放在 application.yml 文件中。
logging:
level:
org.springframework.data.mongodb.core.MongoTemplate: DEBUG