Docs Menu

Docs HomeDevelop ApplicationsMongoDB DriversRust Driver

Usage Examples

On this page

  • Overview
  • How to Use the Usage Examples
  • Available Usage Examples

Usage examples show runnable code examples to demonstrate frequently used MongoDB operations. Each usage example includes the following:

  • Description of the MongoDB operation

  • Asynchronous and synchronous Rust code examples that you can run in your environment

  • Output printed by the code example


Asynchronous and Synchronous APIs

To learn more about selecting and using different runtime APIs in the Rust driver, see the Asynchronous and Synchronous APIs guide.

The usage examples are designed to run operations on a MongoDB deployment that contains the Atlas sample datasets. When you run the example code without the sample data, the output might not match.

Follow this tutorial to set up your MongoDB deployment with the sample data and run the example code in your development environment. Before performing the following actions, ensure that you create an Atlas account and deploy a cluster. For information about setting up an account and a cluster, see the Get Started with Atlas Guide.


Follow the instructions on the Load Sample Data page to load the sample datasets into your database deployment.


Copy the example code from the usage example page and paste it into a new file in your preferred directory.

To learn more about creating an application that uses the Rust driver, follow the Quick Start.


In your example code, replace the connection string placeholder with your MongoDB deployment connection string. For example, if your connection string is "mongodb+srv://mongodb-example:27017", your connection string assignment resembles the following:

let uri = "mongodb+srv://mongodb-example:27017";

Run your application in your development environment. If you run your Rust applications on the command line, run the following command:

cargo run

After completing these steps, you can see the output described in the Expected Output section of the corresponding usage example.

←  What's NewFind a Document →