Connect Cosmos DB from Visual Studio Code

In this article, we will see how to add Cosmos DB extension in Visual Studio Code, and we will connect a local Cosmos DB in VS Code. For that, we are using Cosmos DB Emulator.  


For those who are new to Cosmos DB, Azure Cosmos DB is Microsoft’s globally distributed, multi-model database. With the click of a button, Azure Cosmos DB enables you to elastically and independently scale throughput and storage across any number of Azure’s geographic regions. It offers throughput, latency, availability, and consistency guarantees with comprehensive service level agreements (SLAs), something no other database service can offer. Currently, there are five different types of APIs are supported by Cosmos DB as given below. 

  • SQL 
  • MongoDB 
  • Graph 
  • Table 
  • Cassandra 

Add Cosmos DB extension to Visual Studio Code 

You can click Extensions on the left menu 

You can now search for “Azure Cosmos DB” and click “Install” button 

It takes very few times to complete the installation. 

You can click the below link to check the status of their application build.  (This buildId may be changed in future)

Now you can see a new Azure Cosmos DB menu appeared in the left menu bar. 

You can click that menu to open Cosmos DB explorer. 

We can use Cosmos DB Emulator to create a local Cosmos DB account without creating an Azure account. It is enough for our testing purpose. 

The Azure Cosmos DB Emulator provides a local environment that emulates the Azure Cosmos DB service for development purposes. Using the Azure Cosmos DB Emulator, you can develop and test your application locally, without creating an Azure subscription or incurring any costs. When you’re satisfied with how your application is working in the Azure Cosmos DB Emulator, you can switch to using an Azure Cosmos DB account in the cloud. 

Currently the Data Explorer in the emulator only fully supports clients for SQL API and Azure Cosmos DB’s API for MongoDB. Clients for Table, Graph, and Cassandra APIs are not fully supported. 

You can download Emulator from below link. It is free of cost.  

We can connect to Cosmos DB account by clicking small plug button. 

Currently 4 APIs are supported in VS Code. Cassandra API not yet supported. 

We are choosing MongoDB API now. 

We can copy the connection string from Cosmos DB Emulator and paste to here. Please note there are separate connection strings for SQL API and MongoDB API. 

Our MongoDB is account is connected.  

You can connect the Emulator account in another way also. Right click the “Attached Database Accounts” and again click “Attach Emulator” 

It will ask API type and you can choose which type you want (SQL/Mongo).  

Please note that currently we have only one database “admin” available in the explorer. This is the default database.  

We can create a new database by right clicking the account connection and click “Create Database” 

You can give a name and Enter. 

You can create a collection in the above database. Right click the database and choose “Create Collection” 

Now we can create a new document by right clicking the collection and choose “Create Document” and enter JSON values. I have given 4 keys and values in this JSON file.

We can import existing JSON files also into MongoDB. 

For that, we must create a sample JSON file now. 

I have created 3 keys and values in this JSON file. 

You can now right click the “employee” collection and click “Import Document into a Collection” option. 

We can choose the existing JSON file and import to our collection. 

We can notice that the JSON file data is successfully imported to new document. $oid is the Object Id used in MongoDB as a unique reference for a document. 


In this article, we have seen how to add Azure Cosmos DB extension to Visual Studio Code and then we connected a MongoDB account from Cosmos DB local emulator. We have created a new database, collection and document easily. Finally, we have seen how to import JSON data to a collection very easily. 

We will explore more features of Cosmos DB, Local Emulator, MongoDB and more related to Cosmos DB in upcoming articles. Please send me your valuable comments, so that I can improve my future articles and correct any mistakes I made unintentionally.  

Please follow and like me:

Leave a Reply

Your email address will not be published. Required fields are marked *