How to create Cosmos DB in Azure

How To Create An Azure Cosmos DB Account

This Azure tutorial 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 cosmos db

5. Now, choose the API that suits your requirements. I am choosing here the Core SQL -Recommended option. The benefit of choosing the Core SQL is that 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

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

  • Subscription: Select the Azure Subscription 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.

azure cosmos db account

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

  • Geo-Redundancy: Choose the Disable option.
  • Multi-region Writes: Select the Disable option.
how to create a cosmos db in azure

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

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

10. Finally, you can see the message “Your deployment is complete.” Click the Go to Resource button to navigate to the Azure CosmosDB account you created.

Steps to create Azure Cosmos DB Account

11. You can see the Azure Cosmos DB account page 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. 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 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 select the Manual option, you will have the provision to scale the RU/s by yourself, and if you 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 that the Container name you will enter must be within 1 through 255 characters. Special characters like \, #, ?, / or trailing space are prohibited.

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 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 JSON script below on the right side empty area and click 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, select the New Document again, save the entry with another unique ID, and change the other properties accordingly.

How to query in Cosmos DB

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 below that will show you all the documents 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 articles below

Wrapping Up

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