mongodb - 我怎么知道 Mongodb 查询在哪个节点上运行?
问题描述
连接到 MongoDB 复制集群时,我想知道查询在哪个节点上运行。
我尝试explain()
在 mongo shell 中使用,但 Java 驱动程序似乎不支持此命令。
如何使用 MongoDB Java 驱动程序实现这一点?
解决方案
您可以尝试Command Monitoring
从 MongoDB Java Driver 中使用;
public class CustomCommandListener implements CommandListener {
@Override
public void commandStarted(final CommandStartedEvent event) {
System.out.println(String.format("Sent command '%s:%s' with id %s to database" +
" '%s' on connection '%s' to server '%s'",
event.getCommandName(),
event.getCommand().get(event.getCommandName()),
event.getRequestId(),
event.getDatabaseName(),
event.getConnectionDescription().getConnectionId(),
event.getConnectionDescription().getServerAddress()));
}
@Override
public void commandSucceeded(CommandSucceededEvent event) {
//ignore
}
@Override
public void commandFailed(CommandFailedEvent event) {
//ignore
}
}
还有CommandSucceededEvent
和CommandFailedEvent
,但不管它的结果如何,你都可以用CommandStartedEvent
上面的方法得到一些细节。
然后将此自定义侦听器传递到您的MongoClient
设置中;
ClusterSettings clusterSettings = ClusterSettings.builder().hosts(hostList).build();
MongoClientSettings settings = MongoClientSettings.builder()
.addCommandListener(new CustomCommandListener())
.clusterSettings(clusterSettings)
// other settings
.build();
MongoClient client = MongoClients.create(settings);
推荐阅读
- c# - 动态注册 DLL 后如何在 Unity 中使用 DI
- c# - GetAwaiter().GetResult() 和 .Result 都不适合我,但 Task.Run(await ()=> nameOfMethodTobeCalled()).Result 工作。我没看懂?
- reactjs - Next.js 具有 React 钩子和 localStorage 的持久状态
- c++ - 返回值与写出的值不同
- c# - 可以像在实体框架中一样在 T-SQL 中将表声明为对象吗?
- javascript - 谁能用javascript解释IIFE?
- c++ - 使用 Objective-C++ 创建一个 Metal Window Wrapper
- html - html中的JPEG直播速度慢
- excel - 显示依赖于变量的备用用户窗体
- python - 如何使用 Python 在字典中查找列表内容并将其存储在另一个字典中