Azure Cosmos DB: Get Started Tutorial

Welcome to the Azure Cosmos DB getting started guide! This tutorial will walk you through the essential steps to create and interact with your first Azure Cosmos DB database.

Prerequisites

Before you begin, ensure you have the following:

Step 1: Create an Azure Cosmos DB Account

Navigate to the Azure Portal

Log in to the Azure portal.

Create a new resource

Click on + Create a resource in the top-left corner. Search for "Azure Cosmos DB" and select it.

Configure your Cosmos DB account

On the "Create Azure Cosmos DB account" page, select your preferred API (e.g., Core (SQL) API), subscription, resource group, and provide a unique account name. Choose a region close to your users for the best performance.

For this tutorial, we'll use the Core (SQL) API. Select "No" for "Geo-redundancy" and "Multi-region writes" to keep it simple.

Click Review + create, then Create.

Wait for deployment

The deployment might take a few minutes. Once completed, click Go to resource.

Step 2: Create a Database and Container

Now that you have a Cosmos DB account, let's create a database and a container (which is like a table or collection).

Access Data Explorer

In your Cosmos DB account page, navigate to Data Explorer in the left-hand menu.

Create a new database

Click on New Container. In the "New Container" pane, click New Database. Enter a database ID (e.g., ToDoList) and click OK.

Create a new container

In the "New Container" pane, enter a container ID (e.g., Items). For the Partition key, enter /category (this is crucial for performance and scalability). Click OK.

Important: The partition key is essential for distributing your data and ensuring performance. Choose a high-cardinality property from your items.

Step 3: Add Items to Your Container

Let's add some sample data to your container.

Use the Items tab

In Data Explorer, expand your database and click on the Items container. You should see an "Items" tab.

Create new item

Click on New Item. You can either edit the default JSON or paste your own. Here's an example:

{
    "id": "1",
    "category": "personal",
    "name": "Groceries",
    "description": "Buy milk, eggs, and bread",
    "isComplete": false
}

Click Save.

Add another item

Add another item, ensuring it has a different value for the partition key if you want to see distribution:

{
    "id": "2",
    "category": "work",
    "name": "Project Proposal",
    "description": "Draft the proposal for the new client",
    "isComplete": false
}

Click Save.

Step 4: Query Your Data

Data Explorer also allows you to query your data using SQL-like syntax.

Open New Query

Click on New Query within your Items container view.

Write a query

Enter a simple query to retrieve all items:

SELECT * FROM c

Click Execute. You should see the items you added.

Filter your query

Try filtering the results. For example, to get only personal items:

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

Click Execute.

Next Steps

Congratulations! You've successfully created an Azure Cosmos DB account, database, and container, and you've added and queried data.

From here, you can:

Happy coding with Azure Cosmos DB!