April 29th, 2020

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

Gurpreet Singh
Security Software Engineer

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.

Prerequisites:

  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 https://github.com/Azure-Samples/azure-cosmos-db-mongodb-rust-getting-started

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 main.rs 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 main.rs file in src/ directory and update the connection string (from previous step), database name and collection name as shown in the following code snippet.

https://gist.github.com/gdhuper/e4716c7937af606edea273081e4e63b6

4. Save the main.rs 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/db.rs) 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.

https://gist.github.com/gdhuper/06e3d2800c32942630d9342e4930311a

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

https://gist.github.com/gdhuper/1a9032b6b43b9b0b900847c051682e86

Create a document

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

In main.rs initialize a InsertableTask struct object with appropriate values.

https://gist.github.com/gdhuper/9db249a05e94fd9b91b4ac66b4783d85

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

https://gist.github.com/gdhuper/33596a342212fa9a521ea02ded7d9d1e

Read a document

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

In main.rs, 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”.

https://gist.github.com/gdhuper/08316053695eed8a65506945f183e3d6

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

https://gist.github.com/gdhuper/3e3beb133204e9be8066f7a28f9a58c5

Update a document

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

https://gist.github.com/gdhuper/e67cec8623ae0c969cddb1a77fe61f75

In db.rs, 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.

https://gist.github.com/gdhuper/57f5f41433c63b48c131b26b72c83403

Delete a document

In main.rs, specify the filter for the document to delete.

https://gist.github.com/gdhuper/347dcd3589bd78ed8ac9ba4dafadf039

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

https://gist.github.com/gdhuper/850d0ed468314f183d0d815abcf5ad23

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 main.rs 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

Author

Gurpreet Singh
Security Software Engineer

Software Engineer working in EDG Platform Security Team

1 comment

Discussion is closed. Login to edit/delete existing comments.

  • Stephen Prakash

    Nice Article

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

    currently, I hosted my website in Firebase cloud.

    https://placementmaster.in/