Azure Cosmos DB:SQL API 的 .NET 示例Azure Cosmos DB: .NET examples for the SQL API

azure-cosmos-dotnet-v2 GitHub 存储库中包含可对 Azure Cosmos DB 资源执行 CRUD 操作和其他常见操作的最新 .NET 示例解决方案。The azure-cosmos-dotnet-v2 GitHub repository includes the latest .NET sample solutions to perform CRUD and other common operations on Azure Cosmos DB resources. 本文提供:This article provides:

  • 指向每个示例 C# 项目文件中各项任务的链接。Links to the tasks in each of the example C# project files.
  • 指向相关的 API 参考内容的链接。Links to the related API reference content.

有关 .NET SDK 3.0 版(预览版)的代码示例,请参阅 azure-cosmos-dotnet-v3 GitHub 存储库中的最新示例。For .NET SDK Version 3.0 (Preview) code samples, see the latest samples in the azure-cosmos-dotnet-v3 GitHub repository.

先决条件Prerequisites

已安装包含 Azure 开发工作流的 Visual Studio 2019Visual Studio 2019 with the Azure development workflow installed

Microsoft.Azure.DocumentDB NuGet 包The Microsoft.Azure.DocumentDB NuGet package

Azure 订阅,或免费的 Cosmos DB 试用帐户An Azure subscription or free Cosmos DB trial account

备注

这些示例都是独立的,运行完成后,可通过多次调用 CreateDocumentCollectionAsync() 对其进行设置和清理。The samples are self-contained, and set up and clean up after themselves with multiple calls to CreateDocumentCollectionAsync(). 每次执行此操作时,都会根据集合的性能层,对订阅收取使用 1 小时的费用。Each occurrence bills your subscription for one hour of usage in your collection's performance tier.

数据库示例Database examples

DatabaseManagement 项目示例的 RunDatabaseDemo 方法演示如何执行以下任务。The RunDatabaseDemo method of the sample DatabaseManagement project shows how to do the following tasks. 若要在运行以下示例之前了解 Azure Cosmos 数据库,请参阅使用数据库、容器和项To learn about Azure Cosmos databases before you run the following samples, see Work with databases, containers, and items.

任务Task API 参考API reference
创建数据库Create a database DocumentClient.CreateDatabaseIfNotExistsAsyncDocumentClient.CreateDatabaseIfNotExistsAsync
按 ID 读取数据库Read a database by ID DocumentClient.ReadDatabaseAsyncDocumentClient.ReadDatabaseAsync
Read all the databasesRead all the databases DocumentClient.ReadDatabaseFeedAsyncDocumentClient.ReadDatabaseFeedAsync
删除数据库Delete a database DocumentClient.DeleteDatabaseAsyncDocumentClient.DeleteDatabaseAsync

集合示例Collection examples

CollectionManagement 项目示例的 RunCollectionDemo 方法演示如何执行以下任务。The RunCollectionDemo method of the sample CollectionManagement project shows how to do the following tasks. 若要在运行以下示例之前了解 Azure Cosmos 集合,请参阅使用数据库、容器和项To learn about Azure Cosmos collections before you run the following samples, see Work with databases, containers, and items.

任务Task API 参考API reference
创建集合Create a collection DocumentClient.CreateDocumentCollectionIfNotExistsAsyncDocumentClient.CreateDocumentCollectionIfNotExistsAsync
Change configured performance of a collectionChange configured performance of a collection DocumentClient.ReplaceOfferAsyncDocumentClient.ReplaceOfferAsync
按 ID 获取集合Get a collection by ID DocumentClient.ReadDocumentCollectionAsyncDocumentClient.ReadDocumentCollectionAsync
Read all the collections in a databaseRead all the collections in a database DocumentClient.ReadDocumentCollectionFeedAsyncDocumentClient.ReadDocumentCollectionFeedAsync
删除集合Delete a collection DocumentClient.DeleteDocumentCollectionAsyncDocumentClient.DeleteDocumentCollectionAsync

文档示例Document examples

DocumentManagement 项目示例的 RunDocumentsDemo 方法演示如何执行以下任务。The RunDocumentsDemo method of the sample DocumentManagement project shows how to do the following tasks. 若要在运行以下示例之前了解 Azure Cosmos 文档,请参阅使用数据库、容器和项To learn about Azure Cosmos documents before you run the following samples, see Work with databases, containers, and items.

任务Task API 参考API reference
创建文档Create a document DocumentClient.CreateDocumentAsyncDocumentClient.CreateDocumentAsync
按 ID 读取文档Read a document by ID DocumentClient.ReadDocumentAsyncDocumentClient.ReadDocumentAsync
Read all the documents in a collectionRead all the documents in a collection DocumentClient.ReadDocumentFeedAsyncDocumentClient.ReadDocumentFeedAsync
查询文档Query for documents DocumentClient.CreateDocumentQueryDocumentClient.CreateDocumentQuery
替换文档Replace a document DocumentClient.ReplaceDocumentAsyncDocumentClient.ReplaceDocumentAsync
更新插入文档Upsert a document DocumentClient.UpsertDocumentAsyncDocumentClient.UpsertDocumentAsync
删除文档Delete a document DocumentClient.DeleteDocumentAsyncDocumentClient.DeleteDocumentAsync
使用 .NET 动态对象Working with .NET dynamic objects DocumentClient.CreateDocumentAsyncDocumentClient.CreateDocumentAsync
DocumentClient.ReadDocumentAsyncDocumentClient.ReadDocumentAsync
DocumentClient.ReplaceDocumentAsyncDocumentClient.ReplaceDocumentAsync
使用条件 ETag 检查替换文档Replace document with conditional ETag check DocumentClient.AccessConditionDocumentClient.AccessCondition
Documents.Client.AccessConditionTypeDocuments.Client.AccessConditionType
仅当文档已更改时读取文档Read document only if document has changed DocumentClient.AccessConditionDocumentClient.AccessCondition
Documents.Client.AccessConditionTypeDocuments.Client.AccessConditionType

索引示例Indexing examples

IndexManagement 项目示例的 RunIndexDemo 方法演示如何执行以下任务。The RunIndexDemo method of the sample IndexManagement project shows how to do the following tasks. 若要在运行以下示例之前了解 Azure Cosmos DB 中的索引,请参阅索引策略索引类型索引路径To learn about indexing in Azure Cosmos DB before you run the following samples, see index policies, index types, and index paths.

任务Task API 参考API reference
从索引中排除文档Exclude a document from the index IndexingDirective.ExcludeIndexingDirective.Exclude
使用延迟索引Use Lazy indexing IndexingPolicy.IndexingModeIndexingPolicy.IndexingMode
从索引中排除指定的文档路径Exclude specified document paths from the index IndexingPolicy.ExcludedPathsIndexingPolicy.ExcludedPaths
对哈希索引路径强制执行范围扫描操作Force a range scan operation on a hash indexed path FeedOptions.EnableScanInQueryFeedOptions.EnableScanInQuery
对字符串使用范围索引Use range indexes on strings IndexingPolicy.IncludedPathsIndexingPolicy.IncludedPaths
RangeIndexRangeIndex
执行索引转换Perform an index transform ReplaceDocumentCollectionAsyncReplaceDocumentCollectionAsync

地理空间示例Geospatial examples

示例 Geospatial 项目的 RunDemoAsync 方法演示如何执行以下任务。The RunDemoAsync method of the sample Geospatial project shows how to do the following tasks. 若要在运行以下示例之前了解 GeoJSON 和地理空间数据,请参阅使用地理空间和 GeoJSON 位置数据To learn about GeoJSON and geospatial data before you run the following samples, see Use geospatial and GeoJSON location data.

任务Task API 参考API reference
对新集合启用地理空间索引Enable geospatial indexing on a new collection IndexingPolicyIndexingPolicy
IndexKind.SpatialIndexKind.Spatial
DataType.PointDataType.Point
使用 GeoJSON 点插入文档Insert documents with GeoJSON points DocumentClient.CreateDocumentAsyncDocumentClient.CreateDocumentAsync
DataType.PointDataType.Point
对现有集合启用地理空间索引Enable geospatial indexing on an existing collection DocumentClient.ReplaceDocumentCollectionAsyncDocumentClient.ReplaceDocumentCollectionAsync
DocumentCollection.IndexingPolicyDocumentCollection.IndexingPolicy

查询示例Query examples

示例 Queries 项目的 RunDemoAsync 方法显示了如何使用 SQL 查询语法、查询的 LINQ 提供程序和 Lambda 执行以下任务。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. 若要在运行以下示例之前了解 Azure Cosmos DB 中的 SQL 查询引用,请参阅 Azure Cosmos DB 的 SQL 查询示例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 参考API reference
查询所有文档Query for all documents DocumentQueryable.CreateDocumentQueryDocumentQueryable.CreateDocumentQuery
使用 == 查询等式Query for equality using == DocumentQueryable.CreateDocumentQueryDocumentQueryable.CreateDocumentQuery
使用 != 和 NOT 查询不等式Query for inequality using != and NOT DocumentQueryable.CreateDocumentQueryDocumentQueryable.CreateDocumentQuery
使用 >、<、>=、<= 等范围运算符进行查询Query using range operators like >, <, >=, <= DocumentQueryable.CreateDocumentQueryDocumentQueryable.CreateDocumentQuery
使用范围运算符对字符串进行查询Query using range operators against strings DocumentQueryable.CreateDocumentQueryDocumentQueryable.CreateDocumentQuery
使用 ORDER BY 进行查询Query with ORDER BY DocumentQueryable.CreateDocumentQueryDocumentQueryable.CreateDocumentQuery
使用聚合函数进行查询Query with aggregate functions DocumentQueryable.CreateDocumentQueryDocumentQueryable.CreateDocumentQuery
使用子文档Work with subdocuments DocumentQueryable.CreateDocumentQueryDocumentQueryable.CreateDocumentQuery
使用文档内联接进行查询Query with intra-document Joins DocumentQueryable.CreateDocumentQueryDocumentQueryable.CreateDocumentQuery
使用字符串、数学和数组运算符进行查询Query with string, math, and array operators DocumentQueryable.CreateDocumentQueryDocumentQueryable.CreateDocumentQuery
通过使用 SqlQuerySpec 的参数化 SQL 进行查询Query with parameterized SQL using SqlQuerySpec DocumentQueryable.CreateDocumentQueryDocumentQueryable.CreateDocumentQuery
SqlQuerySpecSqlQuerySpec
使用显式分页进行查询Query with explicit paging DocumentQueryable.CreateDocumentQueryDocumentQueryable.CreateDocumentQuery
并行查询已分区集合Query partitioned collections in parallel DocumentQueryable.CreateDocumentQueryDocumentQueryable.CreateDocumentQuery
使用 ORDER BY 针对已分区集合进行查询Query with ORDER BY for partitioned collections DocumentQueryable.CreateDocumentQueryDocumentQueryable.CreateDocumentQuery

更改源示例Change feed examples

示例 ChangeFeed 项目的 RunDemoAsync 方法演示如何执行以下任务。The RunDemoAsync method of the sample ChangeFeed project shows how to do the following tasks. 若要在运行以下示例之前了解 Azure Cosmos DB 中的更改源,请参阅读取 Azure Cosmos DB 更改源更改源处理器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 参考API reference
读取更改源Read change feed DocumentClient.CreateDocumentChangeFeedQueryDocumentClient.CreateDocumentChangeFeedQuery
读取分区键范围Read partition key ranges DocumentClient.ReadPartitionKeyRangeFeedAsyncDocumentClient.ReadPartitionKeyRangeFeedAsync

更改源处理器示例 ChangeFeedMigrationTool 演示如何使用更改源处理器库将数据复制到另一个 Cosmos 容器。The change feed processor sample, ChangeFeedMigrationTool, shows how to use the change feed processor library to replicate data to another Cosmos container.

服务器端编程示例Server-side programming examples

示例 ServerSideScripts 项目的 RunDemoAsync 方法演示如何执行以下任务。The RunDemoAsync method of the sample ServerSideScripts project shows how to do the following tasks. 若要在运行以下示例之前了解 Azure Cosmos DB 中的服务器端编程,请参阅存储过程、触发器和用户定义的函数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 参考API reference
创建存储过程Create a stored procedure DocumentClient.CreateStoredProcedureAsyncDocumentClient.CreateStoredProcedureAsync
执行存储过程Execute a stored procedure DocumentClient.ExecuteStoredProcedureAsyncDocumentClient.ExecuteStoredProcedureAsync
读取存储过程的文档源Read a document feed for a stored procedure DocumentClient.ReadDocumentFeedAsyncDocumentClient.ReadDocumentFeedAsync
使用 ORDER BY 创建存储过程Create a stored procedure with ORDER BY DocumentClient.CreateStoredProcedureAsyncDocumentClient.CreateStoredProcedureAsync
创建预触发器Create a pre-trigger DocumentClient.CreateTriggerAsyncDocumentClient.CreateTriggerAsync
创建后触发器Create a post-trigger DocumentClient.CreateTriggerAsyncDocumentClient.CreateTriggerAsync
创建用户定义函数 (UDF)Create a user-defined function (UDF) DocumentClient.CreateUserDefinedFunctionAsyncDocumentClient.CreateUserDefinedFunctionAsync

用户管理示例User management examples

示例 UserManagement 项目的 RunDemoAsync 方法演示如何执行以下任务:The RunDemoAsync method of the sample UserManagement project shows how to do the following tasks:

任务Task API 参考API reference
创建用户Create a user DocumentClient.CreateUserAsyncDocumentClient.CreateUserAsync
对集合或文档设置权限Set permissions on a collection or document DocumentClient.CreatePermissionAsyncDocumentClient.CreatePermissionAsync
获取用户权限列表Get a list of a user's permissions DocumentClient.ReadUserAsyncDocumentClient.ReadUserAsync
DocumentClient.ReadPermissionFeedAsyncDocumentClient.ReadPermissionFeedAsync