对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

命令监控

本指南向您展示如何使用 Rust 驱动程序监控驱动程序发送到 MongoDB 部署的命令的结果。

您可以使用有关应用程序中命令事件的信息,也可以监控命令以了解有关驱动程序如何执行这些命令的更多信息。

本指南包括以下部分:

您可以订阅以下一个或多个命令监控事件:

事件名称
说明

在命令启动时创建。

命令成功时创建。

命令不成功时创建。

您可以通过在应用程序中订阅来访问一个或多个命令事件。 以下示例连接到 MongoDB 部署并订阅CommandStartedEvent事件类型:

struct CommandStartHandler;
impl CommandEventHandler for CommandStartHandler {
fn handle_command_started_event(&self, event: CommandStartedEvent) {
eprintln!("Command started: {:?}", event);
}
}
let handler: Arc<dyn CommandEventHandler> = Arc::new(CommandStartHandler);
client_options.command_event_handler = Some(handler);
let client = Client::with_options(client_options)?;
// ... perform actions with the client to generate events

以下部分显示了每种类型的命令监控事件的输出示例。

CommandStartedEvent {
request_id: 12,
db: "testdb",
command_name: "find",
connection: ...,
command: ...,
service_id: ...
}
CommandSucceededEvent {
duration: ...,
reply: ...,
command_name: "find",
request_id: 12,
connection: ...,
service_id: ...,
}
CommandFailedEvent {
duration: ...,
command_name: "find",
failure: ...,
request_id: 12,
connection: ...,
service_id: ...,
}

要了解有关监控 MongoDB 部署的更多信息,请参阅如何监控 MongoDB一文。

要学习;了解有关执行MongoDB操作的更多信息,请参阅增删改查操作部分。

要进一步了解本指南所提及的方法和类型,请参阅以下 API 文档: