Azure Cosmos DB Python examples

Sample solutions that do CRUD operations and other common operations on Azure Cosmos DB resources are included in the azure-documentdb-python GitHub repository. This article provides:

  • Links to the tasks in each of the Python example project files.
  • Links to the related API reference content.

Prerequisites

If you don't have an Azure subscription, create a free account before you begin.

You can Try Azure Cosmos DB for free without an Azure subscription, free of charge and commitments. Or, you can use the Azure Cosmos DB Emulator with a URI of https://localhost:8081. For the key to use with the emulator, see Authenticating requests.

You also need the Python SDK.

Note

Each sample is self-contained; it sets itself up and cleans up after itself. The samples issue multiple calls to CosmosClient.CreateContainer. Each time this is done, your subscription is billed for one hour of usage. For more information about Azure Cosmos DB billing, see Azure Cosmos DB Pricing.

Database examples

The Program.py file of the DatabaseManagement project shows how to do the following tasks. To learn about the Azure Cosmos databases before running the following samples, see Working with databases, containers, and items conceptual article.

Task API reference
Create a database CosmosClient.CreateDatabase
Read a database by ID CosmosClient.ReadDatabase
List databases for an account CosmosClient.ReadDatabases
Delete a database CosmosClient.DeleteDatabase

Collection examples

The Program.py file of the CollectionManagement project shows how to do the following tasks. To learn about the Azure Cosmos collections before running the following samples, see Working with databases, containers, and items conceptual article.

Task API reference
Create a collection CosmosClient.CreateContainer
Read a list of all collections in a database CosmosClient.ReadContainers
Get a collection by ID CosmosClient.ReadContainer
Change the throughput of a collection CosmosClient.ReplaceOffer
Delete a collection CosmosClient.DeleteContainer

Document examples

The Program.py file of the DocumentManagement project shows how to do the following tasks. To learn about the Azure Cosmos documents before running the following samples, see Working with databases, containers, and items conceptual article.

Task API reference
Create a document CosmosClient.CreateItem
Create a collection of documents CosmosClient.CreateItem
Read a document by ID CosmosClient.ReadItem
Read all the documents in a collection CosmosClient.ReadItems
Replace document with conditional ETag check CosmosClient.ReplaceItem

Indexing examples

The Program.py file of the IndexManagement project shows how to do the following tasks. To learn about indexing in Azure Cosmos DB before running the following samples, see indexing policies, indexing types, and indexing paths conceptual articles.

Task API reference
Use manual (instead of automatic) indexing Automatic indexing policy
Exclude specified document paths from the index Indexing policy with excluded paths
Exclude a document from the index IndexingDirective.Exclude
Set indexing mode IndexingMode
Use range indexes on strings Indexing policy with included paths
Perform an index transformation CosmosClient.ReplaceContainer

Query examples

The sample projects also show how to do the following query tasks. To learn about the SQL query reference in Azure Cosmos DB before running the following samples, see SQL query examples conceptual article. To learn about the SQL query reference in Azure Cosmos DB before running the following samples, see SQL query examples conceptual article.

Task API reference
Query an account for a database CosmosClient.QueryDatabases
Query for documents CosmosClient.QueryItems
Force a range scan operation on a hash indexed path HttpHeaders.EnableScanInQuery