Getting Started with Azure Cosmos DB

Welcome to the essential guide for beginning your journey with Azure Cosmos DB, a globally distributed, multi-model database service. This article will walk you through the fundamental concepts and steps needed to get your first Cosmos DB database up and running.

What is Azure Cosmos DB?

Azure Cosmos DB is Microsoft's proprietary globally distributed, multi-model database service. It is the first service to offer multi-master writes globally, and guarantees data processing times within milliseconds. Cosmos DB supports various data models, including document, key-value, graph, and column-family.

Key Concepts

Steps to Get Started

1. Create an Azure Cosmos DB Account

You can create a Cosmos DB account through the Azure portal, Azure CLI, or Azure PowerShell.

Using Azure Portal:

  1. Sign in to the Azure portal.
  2. Click Create a resource.
  3. Search for Azure Cosmos DB and select it.
  4. Click Create.
  5. Fill in the required details (Subscription, Resource group, Account name, API type, Location). For this guide, we'll select the Core (SQL) API.
  6. Configure capacity (provisioned throughput or serverless).
  7. Review and create the account.

2. Create a Database and Container

Once your account is deployed, you'll need to create a database and a container within it.

  1. Navigate to your Cosmos DB account in the Azure portal.
  2. In the left-hand navigation, select Data Explorer.
  3. Click New Container.
  4. Enter a Database ID (e.g., ToDoList) and a Container ID (e.g., Items).
  5. Crucially, select a Partition Key path. For example, /category or /id if each item has a unique identifier.
  6. Configure the throughput for your container (e.g., 400 RU/s).
  7. Click OK.

3. Add Data

You can add data using the Data Explorer in the Azure portal or by writing code.

Using Data Explorer:

  1. In Data Explorer, navigate to your container (e.g., Items).
  2. Click New Item.
  3. Paste or write your JSON document. For example:
    {
        "id": "f04b46a3-0000-0000-0000-000000000001",
        "category": "personal",
        "name": "Groceries",
        "description": "Buy milk and eggs",
        "isComplete": false
    }
  4. Click Save.
Note: Ensure your JSON document includes the id field, which is mandatory for SQL API containers.

4. Query Data

Querying data is done using SQL-like syntax through the Data Explorer or your application code.

Example Query in Data Explorer:

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

Next Steps

View Full Documentation Explore SDKs