{"id":695,"date":"2020-03-19T09:00:39","date_gmt":"2020-03-19T16:00:39","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cosmosdb\/?p=695"},"modified":"2022-09-20T10:38:14","modified_gmt":"2022-09-20T17:38:14","slug":"preview-native-mongo-shell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cosmosdb\/preview-native-mongo-shell\/","title":{"rendered":"Native Mongo shell on Azure Cosmos DB API for MongoDB now in preview"},"content":{"rendered":"<p>We are happy to announce the preview release for native Mongo shell v3.6.8 on the Data Explorer for Azure Cosmos DB\u2019s API for MongoDB!<\/p>\n<p>This will provide you with the native Mongo shell capabilities for database management and CRUD operations. This is available in your Data Explorer either in the <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/cosmos-db\/data-explorer\">Azure Portal<\/a> or <a href=\"https:\/\/cosmos.azure.com\/\">in its stand-alone version<\/a>. Check <a href=\"https:\/\/aka.ms\/cosmos-enable-notebooks\">the highlighted note in this article<\/a> to know which Azure Regions are supported today.<\/p>\n<h3>What is the Azure Cosmos DB API for MongoDB?<\/h3>\n<p>If you haven\u2019t used this API before, this is our implementation of the Wire Protocol defined by MongoDB on top of our Azure Cosmos DB infrastructure. This allows open-source MongoDB drivers and applications using <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/cosmos-db\/mongodb-feature-support-36\">our supported set of features<\/a>. Azure Cosmos DB provides global distribution, elastic scalability, automatic sharding, and <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/cosmos-db\/mongodb-introduction\">many more features<\/a>.<\/p>\n<h3>How can I get the Mongo shell?<\/h3>\n<p>It\u2019s simple! Just check the Data Explorer tab and click on Open Mongo Shell.<\/p>\n<p><img decoding=\"async\" class=\" wp-image-697 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2020\/03\/1-300x173.png\" alt=\"Image 1\" width=\"775\" height=\"447\" srcset=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2020\/03\/1-300x173.png 300w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2020\/03\/1-1024x592.png 1024w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2020\/03\/1-768x444.png 768w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2020\/03\/1.png 1259w\" sizes=\"(max-width: 775px) 100vw, 775px\" \/><\/p>\n<p>Depending on your account\u2019s specific configuration, you might get the following prompt. Click on <strong>Complete setup<\/strong>. This will also <a href=\"https:\/\/azure.microsoft.com\/en-us\/blog\/analyze-and-visualize-your-data-with-azure-cosmos-db-notebooks\/\">enable the Notebooks feature<\/a> in your account!<\/p>\n<p><img decoding=\"async\" class=\" wp-image-696 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2020\/03\/2-300x127.png\" alt=\"Image 2\" width=\"489\" height=\"207\" srcset=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2020\/03\/2-300x127.png 300w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2020\/03\/2-768x326.png 768w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2020\/03\/2.png 1009w\" sizes=\"(max-width: 489px) 100vw, 489px\" \/><\/p>\n<p>After that, the <strong>Mongo shell b<\/strong><strong style=\"font-size: 1rem;\">utton<\/strong><span style=\"font-size: 1rem;\"> will open a ta<\/span><span style=\"font-size: 1rem;\">b with an instance that is already connected to the database. And Voil\u00e1! You can now execute some Mongo scripts directly on your databases and collections.<\/span><img decoding=\"async\" class=\" wp-image-698 aligncenter\" style=\"font-size: 1rem;\" src=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2020\/03\/3-300x95.png\" alt=\"Image 3\" width=\"713\" height=\"226\" srcset=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2020\/03\/3-300x95.png 300w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2020\/03\/3-1024x325.png 1024w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2020\/03\/3-768x244.png 768w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2020\/03\/3.png 1326w\" sizes=\"(max-width: 713px) 100vw, 713px\" \/><\/p>\n<p>It\u2019s also worth noting that this Mongo shell has <strong>version compatibility with MongoDB v3.6<\/strong><strong>.<\/strong><\/p>\n<h3>What can I do with it?<\/h3>\n<p>Here are some Cosmos DB-specific commands of what you can do with the Mongo shell. For a complete list of Mongo shell command extensions for Azure Cosmos DB, see <a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/mongodb-custom-commands\">MongoDB extension commands for Azure Cosmos DB\u2019s API for MongoDB<\/a><\/p>\n<h5><strong>InsertMany as a batch<\/strong><\/h5>\n<p>This isn\u2019t particularly new, but I just wanted to bring up that you can insert an array of objects as a single batch using the <span class=\"lang:default decode:true crayon-inline \">InsertMany()<\/span>\u00a0 function like this:<\/p>\n<pre class=\"line-height:24 left-set:true right-set:true toolbar:0 nums:false lang:js decode:true\">db.test.insertMany([{ _id: 1}, {_id: 2}, {_id: 3}])<\/pre>\n<p>This gives you the advantage of using a single network request to insert a few documents at a time. How did this insert operation perform? Let&#8217;s take a look in the next section.<\/p>\n<h5><strong>Get the request&#8217;s statistics\u00a0<\/strong><\/h5>\n<p>Something that is specific of Cosmos DB is the fact that every database request has an associated <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/cosmos-db\/request-units\">Request Units charge<\/a>. You can retrieve this, along with the name, duration and ActivityId of the last operation that was executed with this command:<\/p>\n<pre class=\"line-height:24 left-set:true right-set:true toolbar:0 nums:false lang:js decode:true\">db.runCommand({ getLastRequestStatistics: 1 } )<\/pre>\n<p>This will output something like this:<\/p>\n<pre class=\"line-height:24 left-set:true right-set:true toolbar:0 nums:false lang:js decode:true\">{\r\n    \"CommandName\" : \"insert\",\r\n    \"RequestCharge\" : 12.28,\r\n    \"RequestDurationInMilliSeconds\" : NumberLong(120),\r\n    \"ActivityId\" : \"956e2374-f302-409f-9c61-ccb8ba9f14db\",\r\n    \"ok\" : 1\r\n}<\/pre>\n<p><span style=\"font-size: 1rem;\">You can use the output of this command to estimate the Request Units that your collection will need. <\/span><span style=\"font-size: 1rem;\">Speaking of RU&#8217;s, here&#8217;s how you can create and assign RU&#8217;s to databases and collections.<\/span><\/p>\n<h5><strong>Create new databases with database-level throughput<\/strong><\/h5>\n<p>You can create resources in the Cosmos DB-style by using the <span class=\"lang:default decode:true crayon-inline \">db.runCommand()<\/span>\u00a0 with a custom action.<\/p>\n<p>For example, if you want to create a new database, <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/cosmos-db\/set-throughput#set-throughput-on-a-database\">and assign Database-level Throughput on it<\/a>, you can issue the <span class=\"lang:default decode:true crayon-inline \">CreateDatabase<\/span> custom command just like this:<\/p>\n<pre class=\"line-height:24 left-set:true right-set:true toolbar:0 nums:false lang:js decode:true\">\/\/ First, execute the use command with a new database name\r\nuse newDatabase;\r\n\/\/ Now run the command to create the database. \r\n\/\/ Using offerThroughput parameter will create it as a database-level throughput type.\r\ndb.runCommand({ customAction: \"CreateDatabase\", offerThroughput: 400});<\/pre>\n<p>If you ever needed to update the throughput of your database, you can just execute the <span class=\"lang:default decode:true crayon-inline \">UpdateDatabase<\/span>\u00a0command.<\/p>\n<pre class=\"line-height:24 left-set:true right-set:true toolbar:0 nums:false lang:js decode:true\">\/\/ First, use an existing database\r\nuse existingDatabase;\r\n\/\/ Now you can update its database-level throughput.\r\ndb.runCommand({ customAction: \"UpdateDatabase\", offerThroughput: 500});<\/pre>\n<p>The following section will show you how to create collections with dedicated throughput.<\/p>\n<h5><strong>Create new collections<\/strong><\/h5>\n<p>For collections, the process looks very similar. You can issue the <span class=\"lang:default decode:true crayon-inline \">CreateCollection<\/span>\u00a0custom command with the below parameters:<\/p>\n<pre class=\"line-height:24 left-set:true right-set:true toolbar:0 nums:false wrap:true lang:js decode:true\">\/\/ This command assumes that you are using the database that you want to create this collection in.\r\n\/\/ If you don't specify the offerThroughput parameter, this collection will need to be in a database with database-level throughput.\r\ndb.runCommand({ customAction: \"CreateCollection\", collection: \"newCollection\", offerThroughput: 1000});<\/pre>\n<p>Alternatively, if you want to create <strong>a sharded collection<\/strong>, also known as unlimited collection, you can issue the same custom command, but now with a shardKey parameter:<\/p>\n<pre class=\"line-height:24 left-set:true right-set:true toolbar:0 nums:false lang:js decode:true \">\/\/ The shardkey specified will be used to distribute the data across multiple shards.\r\ndb.runCommand({ customAction: \"CreateCollection\", collection: \"newCollection\", offerThroughput: 1000, shardKey: \"pk\" });<\/pre>\n<p>After running this, all documents will be required to contain the above specified partition key. Read more on <a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/partition-data\">partitioning in the documentation<\/a>.<\/p>\n<h3>Troubleshooting<\/h3>\n<p>If you experience any connectivity issues with the Mongo shell, just close the tab and open a new one in the Data Explorer. This should solve any connectivity problems.<\/p>\n<p>One feature that currently isn&#8217;t supported if your Cosmos DB account has <a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/vnet-service-endpoint\">VNET integration<\/a>\u00a0enabled. This is currently under progress and should be available sometime soon.<\/p>\n<h3>Next steps<\/h3>\n<ul>\n<li>Check out our <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/cosmos-db\/mongodb-introduction#cosmos-dbs-api-for-mongodb\">QuickStart<\/a> for Azure Cosmos DB&#8217;s API for MongoDB with the Mongo shell!<\/li>\n<li>If you have any feedback, let us know in the comments!<\/li>\n<li>If you have any new feature ideas for Mongo shell or Azure Cosmos DB\u2019s API for MongoDB Let us know at <a href=\"https:\/\/feedback.azure.com\/forums\/263030-azure-cosmos-db?category_id=321994\">our UserVoice page<\/a>.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>We are happy to announce the preview release for native Mongo shell v3.6.8 on the Data Explorer for Azure Cosmos DB\u2019s API for MongoDB! This will provide you with the native Mongo shell capabilities for database management and CRUD operations. This is available in your Data Explorer either in the Azure Portal or in its [&hellip;]<\/p>\n","protected":false},"author":20924,"featured_media":61,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[12,15],"tags":[963,740,289,499,957,1247,1249,286,1245,1246,1251,1248,1250],"class_list":["post-695","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-announcements","category-mongodb-api","tag-api-for-mongodb","tag-appdev","tag-azure","tag-azure-cosmos-db","tag-cosmosdb","tag-data","tag-datatool","tag-migration","tag-mongo","tag-mongodb","tag-mongoshell","tag-tool","tag-tooling"],"acf":[],"blog_post_summary":"<p>We are happy to announce the preview release for native Mongo shell v3.6.8 on the Data Explorer for Azure Cosmos DB\u2019s API for MongoDB! This will provide you with the native Mongo shell capabilities for database management and CRUD operations. This is available in your Data Explorer either in the Azure Portal or in its [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/695","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\/20924"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/comments?post=695"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/695\/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=695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/categories?post=695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/tags?post=695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}