How to create Cosmos DB in Azure

How To Create An Azure Cosmos DB Account

In this Azure tutorial, we will discuss How to create Cosmos DB in Azure. Apart from this, we will also discuss a few other topics below

How to create Cosmos DB in Azure

Well, it’s quite easy to create an Azure Cosmos DB account. Follow the below steps to create an Azure Cosmos DB account.

  1. Log in to the Azure Portal (https://portal.azure.com/)
  2. Click on the Show Portal Menu option from the top left –> Select + Create a Resource option.
create cosmos db in azure

3. Search for Azure Cosmos DB and then click on the search result Azure Cosmos DB.

How To Create Cosmos DB In Azure

4. Now, click on the Create button.

Create Azure Cosmos DB Account

4. Now, choose the API that suits your requirement. I am choosing here Core SQL -Recommended option. The benefit of choosing the Core SQL is, you can use the SQL syntax to query the DB.

The available options are

  • Core (SQL) – Recommended
  • Azure Cosmos DB API for MongoDB
  • Cassandra
  • Azure Table
  • Gremlin (Graph)

Once you have chosen the API based on your need, click on the Create button.

azure cosmosdb

5. On the Create Azure Cosmos DB Account – Core (SQL) page, provide the below details on the Basics tab.

  • Subscription: Select the Azure Subscription that you want to use here to create the Azure Cosmos DB Account.
  • Resource Group: Select the existing resource group or if you don’t have an existing resource group, you can click on the Create new link to create a new resource group.
  • Account Name: Provide a unique name for the Azure Cosmos DB Account.
  • Location: Select the region or location.
  • Capacity mode: Choose the capacity mode.
  • Apply Free Tier Discount: Choose the Apply free tier discount option based on your need.

Click on the Next: Global Distribution button to navigate to the Global Distribution tab.

Creating An Azure Cosmos DB Account in Azure Portal

6. On the Global Distribution tab, do the below changes.

  • Geo-Redundancy: Choose the Disable option.
  • Multi-region Writes: Select the Disable option.
Steps To Create An Azure Cosmos DB Account

7. Keep the other tab options as the default value as it is and then click on the Review + Create button.

8. Now, the system will validate all the details entered by you and will show you validation passed. Then you can able to see the Create button, click on the Create button to create the Azure CosmosDB account.

9. Finally, you can able to see the message “Your deployment is complete”. Click on the Go to Resource button to navigate to the Azure CosmosDB account that you have created just now.

Steps to create Azure Cosmos DB Account

10. You can see the Azure Cosmos DB account page as below.

creating an Azure Cosmos DB Account using Azure Portal

Creating a database and a container

Follow the below steps to create a database and a container. Basically, you will have to use Data Explorer to create the container and the database.

  1. On the Azure Cosmos DB account page, select the Data Explorer from the left navigation and then select the New Container option as highlighted below.
How to add a container in the Azure Cosmos DB account

2. On the New Container window, apply the below settings.

  • Database id: Enter the Database id as “ToDoList”. Note down that the database name you will enter must be within 1 through 255 characters. Special characters like \, #, ?,/ or trailing space are not allowed.
  • Database throughput (autoscale): You can choose Autoscale or Manual based on your need. If you will select the Manual option, you will have the provision to scale the RU/s by yourself and if you will choose autoscale the system will automatically scale the RU/s based on your usage.

You can mention the throughput as 400 RU/s and you have the provision to change it later based on the need.

  • Container id: Enter the Container name as items. Note down that the Container name you will enter must be within 1 through 255 characters. Special characters like \, #, ?,/ or trailing space are not allowed.

Finally, select the Ok button.

Creating a database and a container in Azure Cosmos DB
Creating a database and a container Azure Cosmos DB

Now, the database and the container will get created.

How to Create a database and a container Azure Cosmos DB

We have already created the database and the container and now is the time to add the data to the empty database. Below are the steps you can follow to add the data to the database.

  1. On the Azure Cosmos DB account page, Select the Data Explorer –> Expand items –> Click on New Item
How to Add data to the Azure Cosmos database

2. Now add the below JSON script on the right side empty area and then click on the Save button.

{
    "id": "1",
    "category": "IT",
    "name": "PC",
    "description": "This PC is for Ram.",
    "isComplete": false
}
Adding data to the Azure Cosmos DB

3. If you want to save another entry then select the New Document again and save the entry with another unique id and change the other properties accordingly.

How to query in Cosmos DB

In fact, it’s quite easy to query your data in Azure Cosmos DB. You can use any of the supported SQL queries here to retrieve the result, You can also use the Data Explorer to create stored procedures, and triggers, You can also able to use the filter option to get a specific set of records, etc.

  1. You can get the default query ( SELECT * FROM c ) output like below that will actually show you all the documents that are present on the container.
{
    "id": "1",
    "category": "IT",
    "name": "PC",
    "description": "This PC is for Ram.",
    "isComplete": false,
    "_rid": "FXI0AJaCxzMBAAAAAAAAAA==",
    "_self": "dbs/FXI0AA==/colls/FXI0AJaCxzM=/docs/FXI0AJaCxzMBAAAAAAAAAA==/",
    "_etag": "\"cb00d84c-0000-0700-0000-61360b8a0000\"",
    "_attachments": "attachments/",
    "_ts": 1630931850
}
How to query in cosmos db

2. Now if you want to apply the filter to get a specific set of records, you can apply the filter by clicking on the Edit Filter button.

How to query in Azure cosmos db
{
    "id": "2",
    "category": "ITO",
    "name": "PC",
    "description": "This PC is for Sam.",
    "isComplete": false,
    "_rid": "FXI0AJaCxzMCAAAAAAAAAA==",
    "_self": "dbs/FXI0AA==/colls/FXI0AJaCxzM=/docs/FXI0AJaCxzMCAAAAAAAAAA==/",
    "_etag": "\"d70075a2-0000-0700-0000-613650b00000\"",
    "_attachments": "attachments/",
    "_ts": 1630949552
}

You may also like following the below articles

Wrapping Up

In this article, we have discussed How To Create An Azure Cosmos DB Account. Hope you have enjoyed this article !!!