Introduction
For those 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
Now, search for “Azure Cosmos DB” and click the “Install” button.
It takes a moment to complete the installation.
You can click the below link to check the status of the application build.
https://dev.azure.com/ms-azuretools/AzCode/_build/results?buildId=3206
Now, you can see a new Azure Cosmos DB menu appeared in the left menu bar.
Click that menu to open the Cosmos DB Explorer.
We will use Cosmos DB Emulator to create a local Cosmos DB account without creating an Azure account. It is enough for our testing purposes.
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 the Emulator from the below link. It is free of cost.
https://docs.microsoft.com/en-us/azure/cosmos-db/local-emulator
Connect to Cosmos DB account by clicking the small plug button.
Currently, 4 APIs are supported in VS Code. Cassandra API, however, is not supported yet.
We are choosing MongoDB API now.
Copy the connection string from Cosmos DB Emulator and paste 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 the API type and you can choose a 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 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 choosing “Create Document" and entering the 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.
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. The JSON file data is successfully imported to a new document. $oid is the Object Id used in MongoDB as a unique reference for a document.
Conclusion
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.