Azure Cosmos DB: Quickstart Tutorial

This tutorial guides you through creating and interacting with an Azure Cosmos DB account using the Core (SQL) API in the Azure portal.

Note: This quickstart assumes you have an active Azure subscription. If you don't have one, you can create a free account before you begin.

Prerequisites

Step 1: Create an Azure Cosmos DB Account

Sign in to the Azure Portal

Open your web browser and go to the Azure portal. Sign in with your Azure account credentials.

Create a Cosmos DB Resource

  1. In the Azure portal search bar, type Azure Cosmos DB and select it from the search results.
  2. On the Azure Cosmos DB page, select Create.
  3. On the Select an API page, choose the Core (SQL) API.
  4. Select Create.

The Create Azure Cosmos DB Account page appears.

Configure Basic Settings

On the Basics tab, enter the following details:

Click Review + create.

Review and Create

Review the settings you’ve configured. If everything looks correct, click Create.

The deployment will take a few minutes. Once completed, you'll see a notification indicating that your deployment is complete.

Step 2: Add a Database and Container

Navigate to Your Cosmos DB Account

After the deployment is complete, select Go to resource on the deployment success page. Or, search for your Cosmos DB account in the Azure portal search bar and select it.

Create a Database and Container

  1. In your Cosmos DB account's navigation menu, select Data Explorer.
  2. In Data Explorer, select New Container.
  3. In the New Container pane, configure the following settings:
    • Database id: Select Create new and enter a database ID (e.g., ToDoList).
    • Container id: Enter a container ID (e.g., Items).
    • Partition key: Enter /category. The partition key is crucial for scaling your database.
  4. Select OK.

Data Explorer now shows your new database and container.

Step 3: Add Data to the Container

Add Items

  1. In Data Explorer, expand the ToDoList database and select the Items container.
  2. Select Items in the left navigation.
  3. Select New Item.
  4. Paste the following JSON into the editor:
    
    {
        "id": "6871951838070975",
        "category": "personal",
        "name": "groceries",
        "description": "buy milk and eggs",
        "isComplete": false
    }
    
  5. Select Save.
  6. Add another item by selecting New Item and pasting this JSON:
    
    {
        "id": "6871951838070976",
        "category": "work",
        "name": "project proposal",
        "description": "submit proposal to client",
        "isComplete": false
    }
    
  7. Select Save.

Step 4: Query Data

Query Items

In Data Explorer, with the Items container selected, the following query is automatically executed:


SELECT * FROM c

You can modify this query to filter or sort your data. For example, to find items with the category "personal":


SELECT * FROM c WHERE c.category = "personal"

Press Enter or click the Execute Query button to run your query. The results will appear in the right pane.

Step 5: Clean up Resources

Delete Resource Group

To avoid ongoing charges, it's recommended to delete the resource group you created for this quickstart.

  1. In the Azure portal search bar, type Resource groups and select it.
  2. Select the resource group you created (e.g., cosmosdb-quickstart-rg).
  3. On the resource group's overview page, select Delete resource group.
  4. Confirm the deletion by typing the resource group name and clicking Delete.
Congratulations! You have successfully created an Azure Cosmos DB account, added data, queried it, and cleaned up your resources.
Explore More Azure Cosmos DB Documentation