Azure Cosmos DB.NET V3 SDK (Microsoft.Azure.Cosmos) examples for the SQL API

APPLIES TO: SQL API

The azure-cosmos-dotnet-v3 GitHub repository includes the latest .NET sample solutions to perform CRUD and other common operations on Azure Cosmos DB resources. If you're familiar with the previous version of the .NET SDK, you may be used to the terms collection and document. Because Azure Cosmos DB supports multiple API models, version 3.0 of the .NET SDK uses the generic terms "container" and "item". A container can be a collection, graph, or table. An item can be a document, edge/vertex, or row, and is the content inside a container. This article provides:

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

Prerequisites

Visual Studio 2019 with the Azure development workflow installed

An Azure subscription or free Cosmos DB trial account

Note

The samples are self-contained, and set up and clean up after themselves. Each occurrence bills your subscription for one hour of usage in your container's performance tier.

Database examples

The RunDatabaseDemo method of the sample DatabaseManagement project shows how to do the following tasks. To learn about Azure Cosmos databases before you run the following samples, see Work with databases, containers, and items.

Task API reference
Create a database CosmosClient.CreateDatabaseIfNotExistsAsync
Read a database by ID Database.ReadAsync
Read all the databases for an account CosmosClient.GetDatabaseQueryIterator
Delete a database Database.DeleteAsync

Container examples

The RunContainerDemo method of the sample ContainerManagement project shows how to do the following tasks. To learn about Azure Cosmos containers before you run the following samples, see Work with databases, containers, and items.

Task API reference
Create a container Database.CreateContainerIfNotExistsAsync
Create a container with custom index policy Database.CreateContainerIfNotExistsAsync
Change configured performance of a container Container.ReplaceThroughputAsync
Get a container by ID Container.ReadContainerAsync
Read all the containers in a database Database.GetContainerQueryIterator
Delete a container Container.DeleteContainerAsync

Item examples

The RunItemsDemo method of the sample ItemManagement project shows how to do the following tasks. To learn about Azure Cosmos items before you run the following samples, see Work with databases, containers, and items.

Task API reference
Create an item Container.CreateItemAsync
Read an item by ID container.ReadItemAsync
Query for items container.GetItemQueryIterator
Replace an item container.ReplaceItemAsync
Upsert an item container.UpsertItemAsync
Delete an item container.DeleteItemAsync
Replace an item with conditional ETag check RequestOptions.IfMatchEtag
Partially update (patch) an item container.PatchItemAsync

Indexing examples

The RunIndexDemo method of the sample IndexManagement project shows how to do the following tasks. To learn about indexing in Azure Cosmos DB before you run the following samples, see index policies, index types, and index paths.

Task API reference
Exclude an item from the index IndexingDirective.Exclude
Use Lazy indexing IndexingPolicy.IndexingMode
Exclude specified item paths from the index IndexingPolicy.ExcludedPaths

Query examples

The RunDemoAsync method of the sample Queries project shows how to do the following tasks using the SQL query grammar, the LINQ provider with query, and Lambda. To learn about the SQL query reference in Azure Cosmos DB before you run the following samples, see SQL query examples for Azure Cosmos DB.

Task API reference
Query items from single partition container.GetItemQueryIterator
Query items from multiple partitions container.GetItemQueryIterator
Query using a SQL statement container.GetItemQueryIterator

Change feed examples

The RunBasicChangeFeed method of the sample ChangeFeed project shows how to do the following tasks. To learn about change feed in Azure Cosmos DB before you run the following samples, see Read Azure Cosmos DB change feed and Change feed processor.

Task API reference
Basic change feed functionality Container.GetChangeFeedProcessorBuilder
Read change feed from a specific time Container.GetChangeFeedProcessorBuilder
Read change feed from the beginning ChangeFeedProcessorBuilder.WithStartTime(DateTime)
MIgrate from change feed processor to change feed in V3 SDK Container.GetChangeFeedProcessorBuilder

Server-side programming examples

The RunDemoAsync method of the sample ServerSideScripts project shows how to do the following tasks. To learn about server-side programming in Azure Cosmos DB before you run the following samples, see Stored procedures, triggers, and user-defined functions.

Task API reference
Create a stored procedure Scripts.CreateStoredProcedureAsync
Execute a stored procedure Scripts.ExecuteStoredProcedureAsync
Delete a stored procedure Scripts.DeleteStoredProcedureAsync

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.