Uredi

Deli z drugimi prek


Examples for Azure Cosmos DB for NoSQL SDK for Python

APPLIES TO: NoSQL

Sample solutions that do CRUD operations and other common operations on Azure Cosmos DB resources are included in the main/sdk/cosmos folder of the azure/azure-sdk-for-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

Database examples

The database_management.py Python sample shows how to do the following tasks using CosmosClient methods. To learn about the Azure Cosmos DB databases before running the following samples, see Working with databases, containers, and items conceptual article.

Task API reference
Create a database CosmosClient.create_database
Read a database by ID CosmosClient.get_database_client
Query the databases CosmosClient.query_databases
List databases for an account CosmosClient.list_databases
Delete a database CosmosClient.delete_database

Container examples

The container_management.py Python sample shows how to do the following tasks using DatabaseProxy methods. To learn about the Azure Cosmos DB collections before running the following samples, see Working with databases, containers, and items conceptual article.

Task API reference
Query for a container database.query_containers
Create a container database.create_container
List all the containers in a database database.list_containers
Get a container by its ID database.get_container_client
Manage container's provisioned throughput container.replace_throughput
Delete a container database.delete_container

Item examples

The document_management.py and change_feed_management.py Python samples show how to do the following tasks using ContainerProxy methods. To learn about the Azure Cosmos DB items before running the following samples, see Working with databases, containers, and items conceptual article.

Task API reference
Create items in a container container.create_item
Read an item by its ID container.read_item
Read all the items in a container container.read_all_items
Query an item by its ID container.query_items
Replace an item container.replace_item
Upsert an item container.upsert_item
Delete an item container.delete_item
Get the change feed of items in a container container.query_items_change_feed

Indexing examples

The index_management.py Python sample 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
Exclude a specific item from indexing documents.IndexingDirective.Exclude
Use manual indexing with specific items indexed documents.IndexingDirective.Include
Exclude paths from indexing Define paths to exclude in IndexingPolicy property
Use range indexes on strings Define indexing policy with range indexes on string data type. 'kind': documents.IndexKind.Range, 'dataType': documents.DataType.String
Perform an index transformation database.replace_container (use the updated indexing policy)
Use scans when only hash index exists on the path set the enable_scan_in_query=True and enable_cross_partition_query=True when querying the items

Next steps

Trying to do capacity planning for a migration to Azure Cosmos DB? You can use information about your existing database cluster for capacity planning.