Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I log actual queries to MongoDB with mongo java driver

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.

like image 935
Deplake Avatar asked Jan 18 '18 16:01

Deplake


People also ask

Is there a JDBC driver for MongoDB?

JDBC drivers are Java library files with the extension . jar used by all Java applications to connect to the database. Usually, they are provided by the same company which implemented the MongoDb software. DbSchema Tool already includes an MongoDb driver, which is automatically downloaded when you connect to MongoDb.

How does JDBC connect to MongoDB?

config default directory and add the ISmongodb. jar path to the existing CLASSPATH and CLASS_NAMES entries. CLASSPATH specifies the location of the JDBC Driver for MongoDB database and the CLASS_NAMES specifies the class file that needs to be loaded by the JDBC Connector for connecting to the Database.


1 Answers

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)
like image 193
glytching Avatar answered Sep 18 '22 21:09

glytching