The sample presented in this article is a simple command-line based application that uses the Rust driver for MongoDB. Since Azure Cosmos DB's API for MongoDB is compatible with the MongoDB wire protocol, it is possible for any MongoDB client driver to connect to it.
You will learn how to use the MongoDB Rust driver to interact with Azure Cosmos DB's API for MongoDB by exploring CRUD (create, read, update, delete) operations implemented in the sample code. Finally, you can run the application locally to see it in action.
- An Azure account with an active subscription. Create one for free. Or try Azure Cosmos DB for free without an Azure subscription. You can also use the Azure Cosmos DB Emulator with the connection string
.mongodb://localhost:C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==@localhost:10255/admin?ssl=true
. - Rust (version 1.39 or above)
- Git
You need to create a database account for Azure Cosmos DB for Mongo DB API and retrieve the database key that will allow you to connect to the database in subsequent steps.
Run the following commands to clone the sample repository.
-
Open a command prompt, create a new folder named
git-samples
, then close the command prompt.mkdir "C:\git-samples"
-
Open a git terminal window, such as git bash, and use the
cd
command to change to the new folder to install the sample app.cd "C:\git-samples"
-
Run the following command to clone the sample repository. This command creates a copy of the sample app on your computer.
git clone https://github.com/Azure-Samples/cosmosdb-rust-mongodb-quickstart
To build the binary:
cargo build --release
Export the connection string, MongoDB database, and collection names as environment variables.
export MONGODB_URL="mongodb://<COSMOSDB_ACCOUNT_NAME>:<COSMOSDB_PASSWORD>@<COSMOSDB_ACCOUNT_NAME>.mongo.cosmos.azure.com:10255/?ssl=true&replicaSet=globaldb&maxIdleTimeMS=120000&appName=@<COSMOSDB_ACCOUNT_NAME>@"
The
ssl=true
option is important because of Cosmos DB requirements. For more information, see Connection string requirements.
For the MONGODB_URL
environment variable, replace the placeholders for <COSMOSDB_ACCOUNT_NAME>
and <COSMOSDB_PASSWORD>
<COSMOSDB_ACCOUNT_NAME>
: The name of the Azure Cosmos DB account you created<COSMOSDB_PASSWORD>
: The database key extracted in the previous step
export MONGODB_DATABASE=todos_db
export MONGODB_COLLECTION=todos
You can choose your preferred values for MONGODB_DATABASE
and MONGODB_COLLECTION
or leave them as is.
To run the application, change to the correct folder (where the application binary exists):
cd target/release
To create a todo
./todo create "Create an Azure Cosmos DB database account"
If successful, you should see an output with the MongoDB _id
of the newly created document:
inserted todo with id = ObjectId("5ffd1ca3004cc935004a0959")
Create another todo
./todo create "Get the MongoDB connection string using the Azure CLI"
List all the todo
s
./todo list all
You should see the ones you just added:
todo_id: 5ffd1ca3004cc935004a0959 | description: Create an Azure Cosmos DB database account | status: pending
todo_id: 5ffd1cbe003bcec40022c81c | description: Get the MongoDB connection string using the Azure CLI | status: pending
To update the status of a todo
(for example, change it to completed
status), use the todo
ID as such:
./todo update 5ffd1ca3004cc935004a0959 completed
#output
updating todo_id 5ffd1ca3004cc935004a0959 status to completed
updated status for todo id 5ffd1ca3004cc935004a0959
List only the completed todo
s
./todo list completed
You should see the one you just updated
listing 'completed' todos
todo_id: 5ffd1ca3004cc935004a0959 | description: Create an Azure Cosmos DB database account | status: completed
Delete a todo
using it's ID
./todo delete 5ffd1ca3004cc935004a0959
List the todo
s to confirm
./todo list all
The todo
you just deleted should not be present
Data stored in Azure Cosmos DB is available to view and query in the Azure portal.
To view, query, and work with the user data created in the previous step, login to the Azure portal in your web browser.
In the top Search box, enter Azure Cosmos DB. When your Cosmos account blade opens, select your Cosmos account. In the left navigation, select Data Explorer. Expand your collection in the Collections pane, and then you can view the documents in the collection, query the data, and even create and run stored procedures, triggers, and UDFs.
When you're done with your app and Azure Cosmos DB account, you can delete the Azure resources you created so you don't incur more charges. To delete the resources:
In this quickstart, you learned how to create an Azure Cosmos DB MongoDB API account using the Azure Cloud Shell, and create and run a Rust command-line app to manage todo
s. You can now import additional data to your Azure Cosmos DB account. You can also explore the following resources:
- Pre-migration steps for data migrations from MongoDB to Azure Cosmos DB's API for MongoDB
- Upgrade the MongoDB wire protocol version of your Azure Cosmos DB's API for MongoDB account
- Quickstart: Connect a Go application to Azure Cosmos DB's API for MongoDB
- Consistency levels for Azure Cosmos DB and the API for MongoDB