Dep*_*ake 8 java mongodb mongo-java mongo-java-driver
我想看看 mongo java 驱动程序产生什么查询,但我无法做到这一点。
使用官方文档中的信息,我只能在更新操作执行的日志中看到,但我没有看到此操作的查询。
您可以为org.mongodb
to设置记录器级别DEBUG
,您的 Java 驱动程序将发出这样的详细日志记录:
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
Run Code Online (Sandbox Code Playgroud)
在上面的日志输出中,您可以看到客户端提交的查询的详细信息:
org.mongodb.driver.protocol.command|Sending command {find : BsonString{value='sample'}}
Run Code Online (Sandbox Code Playgroud)
或者,您可以在服务器端启用分析...
db.setProfilingLevel(2)
Run Code Online (Sandbox Code Playgroud)
... 导致 MongoDB 分析器收集针对该数据库的所有操作的数据。
探查器输出(包括客户端提交的查询)将写入system.profile
已启用数据库探查的集合中。
文档中有更多详细信息,但简短摘要是:
// 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)
Run Code Online (Sandbox Code Playgroud)