Azure Cosmos DB Python examples

Tip

The Azure Cosmos DB DocumentDB API or SQL (DocumentDB) API is now known as Azure Cosmos DB SQL API. You don't need to change anything to continue running your apps built with DocumentDB API. The functionality remains the same.

Sample solutions that perform 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.

Alternatively, 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 for this tutorial with a URI of https://localhost:8081 and a key of

C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==

You also need the Python SDK.

Note

Each sample is self-contained, it sets itself up and cleans up after itself. As such, the samples issue multiple calls to document_client.CreateCollection. Each time this is done your subscription is billed for one hour of usage per the performance tier of the collection being created.

Database examples

The Program.py file of the DatabaseManagement project shows how to perform the following tasks:

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

Collection examples

The Program.py file of the CollectionManagement project shows how to perform the following tasks:

Task API reference
Create a collection document_client.CreateCollection
Read a list of all collections in a database document_client.ListCollections
Get a collection by ID document_client.ReadCollection
Get performance tier of a collection DocumentQueryable.QueryOffers
Change performance tier of a collection document_client.ReplaceOffer
Delete a collection document_client.DeleteCollection