Getting started with Azure Cosmos DB using API for MongoDB and Rust

Gurpreet Singh

Rust is becoming increasingly popular both externally and internally at Microsoft due to its performance and safety, especially safe concurrency. In this tutorial, we are going to be writing a Rust application to perform create, read, update, and delete (CRUD) operations on data using Azure Cosmos DB’s API for MongoDB.


  1. rust installed on your machine
  2. An Azure subscription
  3. Azure Cosmos DB account
  4. VS Code, Sublime, or your favorite code editor

Clone the sample application

Clone the sample application from github and update the connection string before you run the application.

1. In your terminal or git bash on windows, run the following command to clone the github repo.

  git clone

2. Navigate to the sample application directory and build the code to download all the dependencies.   cargo build

NOTE: Do not run the application yet.

Update your connection string

Before running the application, replace the connection string in file.

1. To get the connection string for your Azure Cosmos DB account, navigate to Azure portal and go to your Azure Cosmos DB account.

2. Click on Connection String in the left navigation menu, and then copy one of the connection strings as shown in the screenshot.

NOTE: Double check if SSL is enabled (Azure Cosmos DB rejects insecure incoming connections).

Azure portal screen with connection strings shown
Using Azure portal to find connection strings for Azure Cosmos DB

3. In the sample application, open the file in src/ directory and update the connection string (from previous step), database name and collection name as shown in the following code snippet.

4. Save the file.

Review the code

In this section, we are going to walk through some code snippets to learn how database resources are created in rust code.

This rust application uses mongodb rust driver and bson (for serializing/de-serializing data).

Connecting the rust application to Azure Cosmos DB

The following code snippet (src/ connects the rust application to Azure Cosmos DB using the connection string provided.

In the following code snippet, the struct MongoClient has a connect function that takes a connection string and establishes connection to the Azure Cosmos DB server using ClientOptions and Client structs from mongodb rust driver.

Optionally, you can specify which database and collection to store/retrieve documents from in the src/ file.

Create a document

The following code snippets shows how to create a document in the rust application.

In initialize a InsertableTask struct object with appropriate values.

The code that inserts the document in the collection is defined in src/ If successfully inserted, the function returns documentId of the new document created in the collection.

Read a document

The following code snippet shows how to read a document from a collection.

In, specify the key to filter the document on. In the example below, we are retrieving a document that contains the key “title” with value “Pay AmeX bill”.

In, read function takes a filter and returns the document if matched in the collection.

Update a document

In, modify the InsertableTask previously created and pass it to the update function.

In, the update function takes a filter (for the document to update) and updated document as arguments. If successfully updated, the function returns count of documents updated.

Delete a document

In, specify the filter for the document to delete.

In, the delete function takes a document filter and returns number of documents deleted.

Run the app

1. Ensure that rust is installed and is invokable from the terminal/command prompt. Run cargo build to verify. 2. The code in file runs all CRUD operations one after the other. So to make sure the app successfully inserts a document, comment out the code lines that deletes the document (lines 67-71). 3. In your terminal/command prompt, navigate to the root directory of the application where Cargo.toml file is and run the following command:

cargo run

If successful, the app will log result on the console.

4. Review your documents in Azure Cosmos DB Data Explorer to ensure if the document was successfully inserted.

Azure data explorer screen with Azure Cosmos DB content
Azure Data Explorer

1 comment

Comments are closed. Login to edit/delete your existing comments

  • Stephen Prakash 0

    Nice Article

    is there any way to move the firebase DB to Azure Cosmos DB?

    currently, I hosted my website in Firebase cloud.

Feedback usabilla icon