Azure Cosmos DB: Get Started with Your First Database

Welcome to Azure Cosmos DB! This tutorial will guide you through the essential steps to create your first Cosmos DB account, database, and container, and then insert and query your first item.

Step 1: Create an Azure Cosmos DB Account

First, you need an Azure Cosmos DB account. This serves as the top-level resource for your Cosmos DB data.

  1. Navigate to the Azure portal.
  2. Click Create a resource.
  3. Search for "Azure Cosmos DB" and select it.
  4. Click Create.
  5. Select the API you want to use (e.g., Core (SQL)). For this tutorial, we'll assume Core (SQL).
  6. Fill in the required details:
    • Subscription: Choose your Azure subscription.
    • Resource group: Create a new one or select an existing one.
    • Account name: A unique name for your Cosmos DB account.
    • Location: Choose a region close to you.
  7. Review and create the account. Deployment might take a few minutes.

Step 2: Create a Database and Container

Once your account is deployed, you'll create a database and a container within it. A container holds your data.

  1. Go to your newly created Azure Cosmos DB account resource in the Azure portal.
  2. In the left navigation pane, select Data Explorer.
  3. In Data Explorer, select New Container.
  4. Configure the container settings:
    • Database id: Enter a name for your new database (e.g., ToDoList).
    • Container id: Enter a name for your new container (e.g., Items).
    • Partition key: This is crucial for performance. For a simple `ToDoList` scenario, choose /category or /id if you expect unique IDs per item. Let's use /id for this basic example.
  5. Click OK to create the database and container.

Tip

Choosing the right partition key is vital for scalability and performance. Consider your access patterns when defining it.

Step 3: Add Your First Item

Now, let's add a simple JSON document (item) to your container.

  1. In Data Explorer, navigate to your Items container under the ToDoList database.
  2. Click New Item.
  3. A JSON editor will appear. Replace the default content with your item data. For example:
    {
        "id": "task1",
        "category": "personal",
        "description": "Buy groceries",
        "isComplete": false,
        "dueDate": "2023-10-27T10:00:00Z"
    }
  4. Click Save.

Step 4: Query Your Item

Let's use a simple SQL query to retrieve the item you just added.

  1. In Data Explorer, with your Items container selected, click the Explore container button (if not already active).
  2. In the query editor, enter the following SQL query:
    SELECT * FROM c WHERE c.id = "task1"
  3. Click Execute Query.

You should see the item you just added appear in the results below the query editor.

Congratulations!

You've successfully created an Azure Cosmos DB account, database, and container, added your first item, and queried it using the Data Explorer. This is just the beginning of what you can do with Azure Cosmos DB.

Next Steps

  • Explore more advanced query capabilities.
  • Learn about different Cosmos DB APIs (MongoDB, Cassandra, Gremlin).
  • Integrate Cosmos DB with your applications using SDKs.
  • Understand throughput provisioning and scaling.

For more detailed information, please refer to the official Azure Cosmos DB documentation.