Azure Cosmos DB Samples

Getting Started with Cosmos DB

Explore ready‑to‑run code samples that demonstrate how to work with Azure Cosmos DB using .NET, Java, Node.js, Python, and more.

.NET - Create a Database

Learn how to create a Cosmos DB database and container using the .NET SDK.

using Azure.Cosmos;

var client = new CosmosClient(endpoint, key);
var db = await client.CreateDatabaseIfNotExistsAsync("SampleDB");
var container = await db.Database.CreateContainerIfNotExistsAsync("Items", "/partitionKey");
View Details »
Java - Query Items

Execute a SQL query against a container and process results.

CosmosClient client = new CosmosClient(endpoint, key);
CosmosContainer container = client.getDatabase("SampleDB").getContainer("Items");
String query = "SELECT * FROM c WHERE c.status='active'";
CosmosPagedIterable<Item> items = container.queryItems(query, new CosmosQueryRequestOptions(), Item.class);
items.forEach(item -> System.out.println(item));
View Details »
Node.js - Upsert Document

Insert or replace a document with the Node.js SDK.

const { CosmosClient } = require("@azure/cosmos");
const client = new CosmosClient({ endpoint, key });
const container = client.database("SampleDB").container("Items");
await container.items.upsert({ id: "item1", name: "Sample", status: "active" });
View Details »
Python - Change Feed

Consume the change feed to react to inserts and updates.

from azure.cosmos import CosmosClient

client = CosmosClient(endpoint, key)
container = client.get_database_client("SampleDB").get_container_client("Items")
for change in container.query_items_change_feed():
    print(change)
View Details »
Go - Bulk Import

Efficiently load large sets of documents using bulk executor.

import (
    "github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
)

client, _ := azcosmos.NewClientWithKey(endpoint, key, nil)
container, _ := client.NewContainer("SampleDB", "Items")
for _, doc := range docs {
    _, _ = container.CreateItem(context.TODO(), azcosmos.PartitionKey{doc.PartitionKey}, doc, nil)
}
View Details »
PowerShell - Manage Throughput

Adjust RU/s provisioned throughput for a container.

Connect-AzAccount
$ctx = New-AzCosmosDBContext -AccountName $account -ResourceGroupName $rg
Set-AzCosmosDBContainerThroughput -Context $ctx -DatabaseName "SampleDB" -Name "Items" -Throughput 1000
View Details »