how to show query while using query annotations with MongoRepository with spring data
Asked Answered
O

7

59

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?

Osteogenesis answered 9/5, 2016 at 14:13 Comment(0)
L
99

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
Luralurch answered 27/9, 2016 at 9:56 Comment(4)
make sure you have imported log4jLuralurch
I even imported log4j and tried the same solution, but its not working for meUtricle
remove the .MongoTemplate. It works as logging.level.org.springframework.data.mongodb.coreBrigandage
@VishalA check my comment, you may need to adjust your main logger configuration.Petta
P
37

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

logging:
  level:
    org.springframework.data.mongodb.core.MongoTemplate: DEBUG
Partee answered 22/2, 2018 at 11:37 Comment(0)
P
27

For ReactiveMongo add this property to your .properties file

logging.level.org.springframework.data.mongodb.core.ReactiveMongoTemplate=DEBUG
Parody answered 8/2, 2018 at 20:31 Comment(0)
R
9

This is a late answer but I found the correct answer hasn't been given yet based on the question.

The answer already given by the people may be valid for other scenarios. But, if you are using the MongoRepository then the correct configuration would be the following:

logging.level.org.springframework.data.mongodb.repository.query= debug

You are using the Query in your configuration instead of query which is wrong.

When you add correct configuration then the logger would be like:

25-06-2020 17:58:43.301 [http-nio-9001-exec-10] DEBUG o.s.d.m.r.query.MongoQueryCreator.complete(162) - Created query Query: { "customer.id" : 2}, Fields: {}, Sort: {}
Reinforce answered 25/6, 2020 at 12:24 Comment(0)
S
1

The proposed solution is valid. But if you use @DocumentReference you may notice that N+1 queries won't appear in log.

If you want to know if referenced collection was queried, add the following property:

logging.level.org.springframework.data.mongodb.core.convert.MongoDatabaseFactoryReferenceLoader=TRACE
Sathrum answered 23/1, 2023 at 11:37 Comment(0)
R
0

I think that the complete solution is according @Wilder Valera and @Chaojun Zhong

When you want to log for MongoTemplate the answer:

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

When you want to log for ReactiveMongoTemplate you should use:

logging.level.org.springframework.data.mongodb.core.ReactiveMongoTemplate=DEBUG
Renfrew answered 16/3, 2022 at 15:59 Comment(0)
P
0

Keep in mind, that even if you do

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

you still may not be able to see the DEBUG statements if your main logger's configuration set to output only INFO messages. In our case, we had a logback.xml where we had an appender for frameworks with level: INFO. Until we changed this one as well to DEBUG we didn't see the queries from MongoDB:

<appender name="FRAMEWORKS" class="ch.qos.logback.core.ConsoleAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>DEBUG</level>
        </filter>
        ...
</appender>
Petta answered 19/7, 2023 at 15:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.