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.
Before you begin, ensure you have the following:
Log in to the Azure portal.
Click on + Create a resource in the top-left corner. Search for "Azure Cosmos DB" and select it.
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.
The deployment might take a few minutes. Once completed, click Go to resource.
Now that you have a Cosmos DB account, let's create a database and a container (which is like a table or collection).
In your Cosmos DB account page, navigate to Data Explorer in the left-hand menu.
Click on New Container. In the "New Container" pane, click New Database. Enter a database ID (e.g., ToDoList
) and click OK.
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.
Let's add some sample data to your container.
In Data Explorer, expand your database and click on the Items
container. You should see an "Items" tab.
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, 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.
Data Explorer also allows you to query your data using SQL-like syntax.
Click on New Query within your Items
container view.
Enter a simple query to retrieve all items:
SELECT * FROM c
Click Execute. You should see the items you added.
Try filtering the results. For example, to get only personal items:
SELECT * FROM c WHERE c.category = "personal"
Click Execute.
Congratulations! You've successfully created an Azure Cosmos DB account, database, and container, and you've added and queried data.
From here, you can: