For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

Command Monitoring

This guide shows you how to use the Rust driver to monitor the outcome of commands that the driver sends to your MongoDB deployment.

You can use information about command events in your application, or you can monitor commands to learn more about how the driver executes them.

This guide includes the following sections:

You can subscribe to one or more of the following command monitoring events:

Event Name
Description

Created when a command starts.

Created when a command succeeds.

Created when a command does not succeed.

You can access one or more command events by subscribing to them in your application. The following example connects to a MongoDB deployment and subscribes to the CommandStartedEvent event type:

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

The following sections show sample output for each type of command monitoring event.

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: ...,
}

To learn more about monitoring a MongoDB deployment, see the How to Monitor MongoDB article.

To learn more about performing MongoDB operations, see the CRUD Operations section.

To learn more about the methods and types mentioned in this guide, see the following API documentation: