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.
Visual Studio 2019 with the Azure development workflow installed
You can download and use the free Visual Studio 2019 Community Edition. Make sure that you enable Azure development during the Visual Studio setup.
An Azure subscription or free Cosmos DB trial account
You can activate Visual Studio subscriber benefits: Your Visual Studio subscription gives you credits every month, which you can use for paid Azure services.
You can Try Azure Cosmos DB for free without an Azure subscription, free of charge and commitments, or create an Azure Cosmos DB free tier account, with the first 400 RU/s and 5 GB of storage for free. You can also use the Azure Cosmos DB Emulator with a URI of
https://localhost:8081. For the key to use with the emulator, see Authenticating requests.
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.
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.
|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|
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.
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.
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.
|Exclude an item from the index||IndexingDirective.Exclude|
|Use Lazy indexing||IndexingPolicy.IndexingMode|
|Exclude specified item paths from the index||IndexingPolicy.ExcludedPaths|
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.
|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.
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.
|Create a stored procedure||Scripts.CreateStoredProcedureAsync|
|Execute a stored procedure||Scripts.ExecuteStoredProcedureAsync|
|Delete a stored procedure||Scripts.DeleteStoredProcedureAsync|
Trying to do capacity planning for a migration to Azure Cosmos DB? You can use information about your existing database cluster for capacity planning.
- If all you know is the number of vcores and servers in your existing database cluster, read about estimating request units using vCores or vCPUs
- If you know typical request rates for your current database workload, read about estimating request units using Azure Cosmos DB capacity planner