Cosmos DB Database Cheatsheet

Microsoft Azure Cosmos DB is a globally distributed, multi-model database service designed for high-performance applications. Whether you’re a seasoned developer or just getting started, having a cheatsheet can be immensely helpful. This cheatsheet provides a quick reference guide for common Cosmos DB tasks and queries.

1. Getting Started

1.1 Create a Cosmos DB Account

az cosmosdb create --name <account-name> --resource-group <resource-group-name> --kind GlobalDocumentDB

1.2 Create a Database and Container

az cosmosdb database create --account-name <account-name> --name <database-name>
az cosmosdb collection create --account-name <account-name> --database-name <database-name> --collection-name <container-name> --partition-key-path /<partition-key>

2. Data Manipulation

2.1 Insert a Document

az cosmosdb document create --account-name <account-name> --database-name <database-name> --collection-name <container-name> --json-document "<json-document>"

2.2 Query Documents

az cosmosdb sql query --account-name <account-name> --database-name <database-name> --collection-name <container-name> --query "<SQL-query>"

2.3 Update a Document

az cosmosdb document update --account-name <account-name> --database-name <database-name> --collection-name <container-name> --json-document "<updated-json-document>"

2.4 Delete a Document

az cosmosdb document delete --account-name <account-name> --database-name <database-name> --collection-name <container-name> --id <document-id>

3. Indexing

3.1 Enable Indexing

az cosmosdb collection update --account-name <account-name> --database-name <database-name> --collection-name <container-name> --indexing-policy "<indexing-policy>"

3.2 Exclude Paths from Indexing

az cosmosdb collection update --account-name <account-name> --database-name <database-name> --collection-name <container-name> --indexing-policy "<indexing-policy-with-excluded-paths>"

4. Scaling

4.1 Scale Throughput

az cosmosdb collection update --account-name <account-name> --database-name <database-name> --collection-name <container-name> --throughput <new-throughput>

4.2 Autoscale

az cosmosdb collection update --account-name <account-name> --database-name <database-name> --collection-name <container-name> --autoscale-max-throughput <max-throughput>

5. Backup and Restore

5.1 Trigger a Backup

az cosmosdb sql trigger-backup --account-name <account-name> --database-name <database-name> --collection-name <container-name> --backup-name <backup-name>

5.2 Restore from Backup

az cosmosdb sql restore --account-name <account-name> --database-name <database-name> --collection-name <container-name> --restore-source <restore-source> --restore-timestamp <restore-timestamp>

This Cosmos DB cheatsheet is designed to make your development tasks easier and more efficient. Whether you’re working with data manipulation, indexing, scaling, or backup and restore, these quick reference commands will help streamline your workflow. Keep this cheatsheet handy for a smooth Cosmos DB experience!

Remember to check the official documentation for the most up-to-date information and additional details.

FAQ

1. What is Cosmos DB?

Azure Cosmos DB is a globally distributed, multi-model database service by Microsoft Azure. It supports multiple data models including document, key-value, graph, and column-family, making it suitable for a variety of applications.

2. How do I create a Cosmos DB account?

Use the Azure CLI with the command az cosmosdb create specifying the account name and resource group. Ensure you choose the appropriate kind, such as GlobalDocumentDB for a document database.

3. Can I scale Cosmos DB throughput?

Yes, you can scale throughput using the Azure CLI with the command az cosmosdb collection update. Adjust the throughput parameter to your desired value. Additionally, Cosmos DB supports autoscale for dynamic throughput adjustments

4. How do I query documents in Cosmos DB?

Use the Azure CLI with the command az cosmosdb sql query specifying the account name, database name, collection name, and the query in SQL-like syntax.

5. Is it possible to backup and restore data in Cosmos DB?

Yes, you can trigger a backup using the command az cosmosdb sql trigger-backup and restore using az cosmosdb sql restore. Ensure you provide the necessary details such as account name, database name, collection name, backup name, and restore source.