Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log the queries executed by Spring Data MongoDB?

Is there a way to find the query that was run against the database in spring data mongodb?

I know this is possible in case of JPA. So was just wondering if there was something similar in Spring data MongoDB?

like image 766
Sam Avatar asked Mar 07 '18 02:03

Sam


People also ask

Can I use spring data JPA with MongoDB?

Yes, DataNucleus JPA allows it, as well as to many other databases.

What is @document annotation in spring boot?

@Document is an annotation provided by Spring data project. It is used to identify a domain object, which is persisted to MongoDB. So you can use it to map a Java class into a collection inside MongoDB. If you don't use Spring Data, you don't need this annotation.

How does MongoDB reduce query execution time?

Steps taken to try to reduce execution time: Only necessary fields are projected which reduced the time by 10-30% depending on the query. Tried to reduce the size of each document to only contain necessary and non-empty fields. Tried to make tags more selective.


2 Answers

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

To clarify Oliver's answer add that to the application.yml file or properties file.

like image 57
Dylan Avatar answered Nov 11 '22 19:11

Dylan


The easiest way is to set the log level to DEBUG for org.springframework.data.mongodb.

like image 25
Oliver Drotbohm Avatar answered Nov 11 '22 17:11

Oliver Drotbohm