{"id":899,"date":"2020-04-29T09:00:58","date_gmt":"2020-04-29T16:00:58","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cosmosdb\/?p=899"},"modified":"2020-05-29T12:02:01","modified_gmt":"2020-05-29T19:02:01","slug":"mongodb-and-rust","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cosmosdb\/mongodb-and-rust\/","title":{"rendered":"Getting started with Azure Cosmos DB using API for MongoDB and Rust"},"content":{"rendered":"<p>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&#8217;s API for MongoDB.<\/p>\n<h3>Prerequisites:<\/h3>\n<ol>\n<li><a href=\"https:\/\/www.rust-lang.org\/tools\/install\">rust<\/a>\u00a0installed on your machine<\/li>\n<li><a href=\"https:\/\/azure.microsoft.com\/en-us\/free\/\">An Azure subscription<\/a><\/li>\n<li><a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/cosmos-db\/create-mongodb-golang#create-a-database-account\">Azure Cosmos DB account<\/a><\/li>\n<li><a href=\"https:\/\/code.visualstudio.com\/\">VS Code<\/a>, <a href=\"https:\/\/www.sublimetext.com\/\">Sublime<\/a>, or your favorite code editor<\/li>\n<\/ol>\n<h3>Clone the sample application<\/h3>\n<p>Clone the sample application from <a href=\"https:\/\/github.com\/Azure-Samples\/azure-cosmos-db-mongodb-rust-getting-started\">github<\/a> and update the connection string before you run the application.<\/p>\n<p>1. In your terminal or git bash on windows, run the following command to clone the github repo.<\/p>\n<p><code>\u00a0 git clone https:\/\/github.com\/Azure-Samples\/azure-cosmos-db-mongodb-rust-getting-started<\/code><\/p>\n<p>2. Navigate to the sample application directory and build the code to download all the dependencies.\n<code>\u00a0 cargo build<\/code><\/p>\n<p><strong>NOTE: Do not run the application yet.<\/strong><\/p>\n<h3>Update your connection string<\/h3>\n<p>Before running the application, replace the connection string in <code>main.rs<\/code> file.<\/p>\n<p>1. To get the connection string for your Azure Cosmos DB account, navigate to Azure portal and go to your Azure Cosmos DB account.<\/p>\n<p>2. Click on Connection String in the left navigation menu, and then copy one of the connection strings as shown in the screenshot.<\/p>\n<p><strong>NOTE: Double check if SSL is enabled (Azure Cosmos DB rejects insecure incoming connections).<\/strong><\/p>\n<p><figure id=\"attachment_916\" aria-labelledby=\"figcaption_attachment_916\" class=\"wp-caption alignnone\" ><img decoding=\"async\" class=\"wp-image-916 size-full\" src=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2020\/04\/cosmosconnectionstring.png\" alt=\"Azure portal screen with connection strings shown\" width=\"1597\" height=\"715\" srcset=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2020\/04\/cosmosconnectionstring.png 1597w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2020\/04\/cosmosconnectionstring-300x134.png 300w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2020\/04\/cosmosconnectionstring-1024x458.png 1024w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2020\/04\/cosmosconnectionstring-768x344.png 768w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2020\/04\/cosmosconnectionstring-1536x688.png 1536w\" sizes=\"(max-width: 1597px) 100vw, 1597px\" \/><figcaption id=\"figcaption_attachment_916\" class=\"wp-caption-text\">Using Azure portal to find connection strings for Azure Cosmos DB<\/figcaption><\/figure><\/p>\n<p>3. In the sample application, open the <code>main.rs<\/code> file in <code>src\/<\/code> directory and update the connection string (from previous step), database name and collection name as shown in the following code snippet.<\/p>\n<p>https:\/\/gist.github.com\/gdhuper\/e4716c7937af606edea273081e4e63b6<\/p>\n<p>4. Save the <code>main.rs<\/code> file.<\/p>\n<h3>Review the code<\/h3>\n<p>In this section, we are going to walk through some code snippets to learn how database resources are created in rust code.<\/p>\n<p>This rust application uses <a href=\"https:\/\/docs.rs\/mongodb\/0.9.2\/mongodb\/\">mongodb<\/a> rust driver and <a href=\"https:\/\/docs.rs\/bson\/0.14.1\/bson\/\">bson<\/a> (for serializing\/de-serializing data).<\/p>\n<h4>Connecting the rust application to Azure Cosmos DB<\/h4>\n<p>The following code snippet (<code>src\/db.rs<\/code>) connects the rust application to Azure Cosmos DB using the connection string provided.<\/p>\n<p>In the following code snippet, the struct <code>MongoClient<\/code> has a <code>connect<\/code> function that takes a connection string and establishes connection to the Azure Cosmos DB server using <a href=\"https:\/\/docs.rs\/mongodb\/0.9.2\/mongodb\/options\/struct.ClientOptions.html\"><code>ClientOptions<\/code><\/a> and <a href=\"https:\/\/docs.rs\/mongodb\/0.9.2\/mongodb\/struct.Client.html\">Client<\/a>\u00a0structs from <a href=\"https:\/\/docs.rs\/mongodb\/0.9.2\/mongodb\/\">mongodb <\/a>rust driver.<\/p>\n<p>https:\/\/gist.github.com\/gdhuper\/06e3d2800c32942630d9342e4930311a<\/p>\n<p>Optionally, you can specify which database and collection to store\/retrieve documents from in the <code>src\/main.rs<\/code> file.<\/p>\n<p>https:\/\/gist.github.com\/gdhuper\/1a9032b6b43b9b0b900847c051682e86<\/p>\n<h4>Create a document<\/h4>\n<p>The following code snippets shows how to create a document in the rust application.<\/p>\n<p>In <code>main.rs<\/code> initialize a <code>InsertableTask<\/code> struct object with appropriate values.<\/p>\n<p>https:\/\/gist.github.com\/gdhuper\/9db249a05e94fd9b91b4ac66b4783d85<\/p>\n<p>The code that inserts the document in the collection is defined in <code>src\/db.rs<\/code>. If successfully inserted, the function returns documentId of the new document created in the collection.<\/p>\n<p>https:\/\/gist.github.com\/gdhuper\/33596a342212fa9a521ea02ded7d9d1e<\/p>\n<h4>Read a document<\/h4>\n<p>The following code snippet shows how to read a document from a collection.<\/p>\n<p>In <code>main.rs,<\/code>\u00a0specify the key to filter the document on. In the example below, we are retrieving a document that contains the key &#8220;title&#8221; with value &#8220;Pay AmeX bill&#8221;.<\/p>\n<p>https:\/\/gist.github.com\/gdhuper\/08316053695eed8a65506945f183e3d6<\/p>\n<p>In <code>db.rs<\/code>, read function takes a filter and returns the document if matched in the collection.<\/p>\n<p>https:\/\/gist.github.com\/gdhuper\/3e3beb133204e9be8066f7a28f9a58c5<\/p>\n<h4>Update a document<\/h4>\n<p>In <code>main.rs<\/code>, modify the <code>InsertableTask<\/code> previously created and pass it to the update function.<\/p>\n<p>https:\/\/gist.github.com\/gdhuper\/e67cec8623ae0c969cddb1a77fe61f75<\/p>\n<p>In <code>db.rs<\/code>, 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.<\/p>\n<p>https:\/\/gist.github.com\/gdhuper\/57f5f41433c63b48c131b26b72c83403<\/p>\n<h4>Delete a document<\/h4>\n<p>In <code>main.rs<\/code>, specify the filter for the document to delete.<\/p>\n<p>https:\/\/gist.github.com\/gdhuper\/347dcd3589bd78ed8ac9ba4dafadf039<\/p>\n<p>In <code>db.rs<\/code>, the delete function takes a document filter and returns number of documents deleted.<\/p>\n<p>https:\/\/gist.github.com\/gdhuper\/850d0ed468314f183d0d815abcf5ad23<\/p>\n<h3>Run the app<\/h3>\n<p>1. Ensure that rust is installed and is invokable from the terminal\/command prompt. Run <code>cargo build<\/code> to verify.\n2. The code in <code>main.rs<\/code> file runs all CRUD operations one after the other. So to make sure the app successfully inserts a document, <strong>comment out the code lines that deletes the document (lines 67-71).<\/strong>\n3. In your terminal\/command prompt, navigate to the root directory of the application where <code>Cargo.toml<\/code> file is and run the following command:<\/p>\n<p><code>cargo run<\/code><\/p>\n<p>If successful, the app will log result on the console.<\/p>\n<p>4. Review your documents in Azure Cosmos DB Data Explorer to ensure if the document was successfully inserted.<\/p>\n<p><figure id=\"attachment_919\" aria-labelledby=\"figcaption_attachment_919\" class=\"wp-caption alignnone\" ><img decoding=\"async\" class=\"wp-image-919 size-full\" src=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2020\/04\/cosmosdatapreview.png\" alt=\"Azure data explorer screen with Azure Cosmos DB content\" width=\"1590\" height=\"643\" srcset=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2020\/04\/cosmosdatapreview.png 1590w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2020\/04\/cosmosdatapreview-300x121.png 300w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2020\/04\/cosmosdatapreview-1024x414.png 1024w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2020\/04\/cosmosdatapreview-768x311.png 768w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2020\/04\/cosmosdatapreview-1536x621.png 1536w\" sizes=\"(max-width: 1590px) 100vw, 1590px\" \/><figcaption id=\"figcaption_attachment_919\" class=\"wp-caption-text\">Azure Data Explorer<\/figcaption><\/figure><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;s API for MongoDB. Prerequisites: rust\u00a0installed on your machine [&hellip;]<\/p>\n","protected":false},"author":24831,"featured_media":61,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[15,19],"tags":[963,740,499,940],"class_list":["post-899","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mongodb-api","category-tips-and-tricks","tag-api-for-mongodb","tag-appdev","tag-azure-cosmos-db","tag-rust"],"acf":[],"blog_post_summary":"<p>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&#8217;s API for MongoDB. Prerequisites: rust\u00a0installed on your machine [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/899","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/users\/24831"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/comments?post=899"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/899\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media\/61"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media?parent=899"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/categories?post=899"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/tags?post=899"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}