10

I want to see what queries mongo java driver produce, but I'm not able to do that.

Using information from the official documentation I'm able just see in the log that update operation executes, but I don't see the query of this operation.

Deplake
  • 865
  • 2
  • 9
  • 22

2 Answers2

16

You can set the logger level for org.mongodb to DEBUG and your Java driver will emit detailed logging like this:

2018-01-18 16:51:07|[main]|[NA]|INFO |org.mongodb.driver.connection|Opened connection [connectionId{localValue:2, serverValue:39}] to localhost:27017  
2018-01-18 16:51:07|[main]|[NA]|DEBUG|org.mongodb.driver.protocol.insert|Inserting 1 documents into namespace stackoverflow.sample on connection [connectionId{localValue:2, serverValue:39}] to server localhost:27017  
2018-01-18 16:51:07|[main]|[NA]|DEBUG|org.mongodb.driver.protocol.insert|Insert completed  
2018-01-18 16:51:07|[main]|[NA]|DEBUG|org.mongodb.driver.protocol.command|Sending command {find : BsonString{value='sample'}} to database stackoverflow on connection [connectionId{localValue:2, serverValue:39}] to server localhost:27017  
2018-01-18 16:51:07|[main]|[NA]|DEBUG|org.mongodb.driver.protocol.command|Command execution completed  
2018-01-18 16:51:07|[main]|[NA]|DEBUG|org.mongodb.driver.protocol.command|Sending command {findandmodify : BsonString{value='sample'}} to database stackoverflow on connection [connectionId{localValue:2, serverValue:39}] to server localhost:27017  
2018-01-18 16:51:07|[main]|[NA]|DEBUG|org.mongodb.driver.protocol.command|Command execution completed  

In the above log output you can see the details of a query submitted by the client:

org.mongodb.driver.protocol.command|Sending command {find : BsonString{value='sample'}}

Alternatively, you can enable profiling on the server side ...

db.setProfilingLevel(2)

... causes the MongoDB profiler to collect data for all operations against that database.

The profiler output (which includes the query submitted by the client) is written to the system.profile collection in whichever database profiling has been enabled.

More details in the docs but the short summary is:

// turn up the logging
db.setProfilingLevel(2)

// ... run some commands

// find all profiler documents, most recent first
db.system.profile.find().sort( { ts : -1 } )

// turn down the logging
db.setProfilingLevel(0)
glytching
  • 44,936
  • 9
  • 114
  • 120
  • I have done all the steps you provided. Now in db I can see profiling here: db.system.profile.find().sort( { ts : -1 } ) But in java log I see just next: 1) Sending command {findandmodify : BsonString{value='blabla'}} to database test on connection [connectionId{localValue:10, serverValue:718}] to server 127.0.0.1:27018 2) Command execution completed No real query, as in your example. Your query appears in the o.g.sandbox.mongo.MongoClientTest. Is it your class and are you logging it by hands? – Deplake Jan 19 '18 at 08:42
  • Yes, `o.g.sandbox.mongo.MongoClientTest` is my own test class. I have updated the answer to remove this possible confusion. The other logs events being shown in my answer **do** include details for the MongoDB `find` command e.g. `Sending command {find : BsonString{value='sample'}}`. – glytching Jan 19 '18 at 08:54
  • But I want exact query in log, like you had in your MongoClientTest class – Deplake Jan 19 '18 at 08:58
  • This: `{find : BsonString{value='sample'}}` is the query in the form provided by the Java driver. The other logs in my original answer, the ones from "o.g.sandbox.mongo.MongoClientTest" were not queries they were documents i.e. the document which was retrieved from the server. The code which produced those logs statements looked like this: `logger.info(document.toJson()`. The exact queries in their raw form are in the profiler documents in the `system.profile` collection. – glytching Jan 19 '18 at 09:07
  • I see, thanks. Just wanted something like system.profile in the java log and not in db – Deplake Jan 19 '18 at 09:14
  • For me it worked with org.mongodb and not com.mongodb. – Mohamed Gara Mar 20 '20 at 18:10
  • If the query is long, it is not displayed fully. Do you know how to log the full query? – Noel Oct 20 '20 at 11:26
  • The log shown in this answer is still not as detailed as in the documentation. Upgrade your driver version to see more details. – Wit Jun 17 '22 at 04:26
1

If you're using Spring Boot 1.5.x (I'm on 1.5.19) you'll need to override the version of org.mongodb:mongodb-driver to at least version 3.7.0 to get the additional info in the logs.

See this ticket for more details: https://jira.mongodb.org/browse/JAVA-2698

jeremyt
  • 510
  • 4
  • 6
  • I my case I also had to add dependency to the same version of `org.mongodb:mongo-java-driver` in order to see detailed logs like in the logging documentation. – Wit Jun 17 '22 at 04:23