How to create Cosmos DB in Azure

Recently, I got the requirement for creating a Cosmos DB in Azure. This Azure tutorial will discuss how to create Cosmos DB in Azure. Additionally, we will discuss how to query in Cosmos DB.

How to create Cosmos DB in Azure

To create Cosmos DB in Azure, follow the steps below.

  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 selecting the Core SQL – Recommended option here. 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 needs, click on the Create button.

how to create a cosmos db in azure

6. On the Create Azure Cosmos DB Account – Core (SQL) page, provide the details below 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 click the Create new link to create a new one.
  • 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: Select the ‘Apply free tier discount’ option based on your specific needs.

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

azure cosmos db account

7. On the Global Distribution tab, make the following 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 their default values, and then click the Review + Create button.

9. Now, the system will validate all the details you entered and show you that the validation passed. Then, you will be able to see the “Create” button. Click on the Create button to create the Azure Cosmos DB 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 steps below 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 following settings.

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

You can specify the throughput as 400 RU/s, and you have the provision to adjust it later as needed.

  • Container ID: Enter the Container name as items. Note that the Container name you will enter must be between 1 and 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 be created.

How to Create a database and a container Azure Cosmos DB

We have already created the database and the container; now is the time to add 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 to 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 ‘New Document’ again, save the entry with a unique ID, and update 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 use the filter option to get a specific set of records, etc.

  1. You can get the default query ( SELECT * FROM c ) output below, which will show you all the documents in 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 cosmos db in azure portal
{
    "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
}

Wrapping Up

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

You may also like the following articles below

Azure Virtual Machine

DOWNLOAD FREE AZURE VIRTUAL MACHINE PDF

Download our free 25+ page Azure Virtual Machine guide and master cloud deployment today!