Getting started with Azure Cosmos DB using API for MongoDB and Rust
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.
- rust installed on your machine
- An Azure subscription
- Azure Cosmos DB account
- 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.
NOTE: Do not run the application yet.
Update your connection string
Before running the application, replace the connection string in
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).
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.
4. Save the
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.
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.
Optionally, you can specify which database and collection to store/retrieve documents from in the
Create a document
The following code snippets shows how to create a document in the rust application.
main.rs initialize a
InsertableTask struct object with appropriate values.
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.
Read a document
The following code snippet shows how to read a document from a collection.
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”.
db.rs, read function takes a filter and returns the document if matched in the collection.
Update a document
main.rs, modify the
InsertableTask previously created and pass it to the update function.
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.
Delete a document
main.rs, specify the filter for the document to delete.
db.rs, 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
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:
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.