how to show query while using query annotations with MongoRepository with spring data

泄露秘密 提交于 2019-12-30 01:36:18

问题


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);
}

and it could work, but i wonder know the exactly query it accessing 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?


回答1:


I add the line (below) in application.properties and works fine:

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



回答2:


Also, you can use a yml config file, put it in your application.yml file.

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



回答3:


For ReactiveMongo add this property to your .properties file

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


来源:https://stackoverflow.com/questions/37118047/how-to-show-query-while-using-query-annotations-with-mongorepository-with-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!