c# - 如何确保 Cosmos DB 中属性的唯一性

标签 c# azure azure-cosmosdb

我们要求集合文档中特定字段的值必须是唯一的。该集合已分区,因此我无法在每次插入之前编写触发器或 SP 来确保这一点(因为我想确保整个集合的唯一性,而不仅仅是分区)。

我遇到了以下链接,其中提到有一个可以添加到集合中的 uniqueKeyPolicy ,但在官方文档中看不到任何示例或更多信息。

最好的方法是什么?

我查到的信息: https://github.com/Azure/azure-documentdb-dotnet/blob/master/changelog.md - 添加了通过使用 DocumentCollection 上的 UniqueKeyPolicy 属性为文档指定唯一索引的功能。

https://learn.microsoft.com/en-us/dotnet/api/microsoft.azure.documents.uniquekeypolicy.uniquekeys?view=azure-dotnet

https://learn.microsoft.com/en-us/dotnet/api/microsoft.azure.documents.uniquekey?view=azure-dotnet

更新:经过一番尝试,我能够在创建新集合(非分区)时使用 .NET 代码中的 DocumentClient 来执行此操作。但是有没有办法为现有集合添加此约束。我也无法从门户网站找到执行此操作的方法。我应该寻找什么特别的地方吗?

最佳答案

似乎无法进行跨分区唯一键,并且只能在集合创建期间定义唯一键。来自 the documentation :

Unique keys must be defined when the container is created, and the unique key is scoped to the partition key. To build on the earlier example, if you partition based on zip code, you could have the records from the table duplicated in each partition.

Existing containers cannot be updated to use unique keys.

关于c# - 如何确保 Cosmos DB 中属性的唯一性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47430896/

相关文章:

c# - 从 tryParse() 返回 errorInfo

Azure PowerShell::如何打印属性列表

Azure Cosmos DB 模拟器 : Unauthorized after upgrate to 2. 9.2

Azure cosmos db gremline批量导入csv

c# - ANTLR 可与 C# 一起使用

c# - 在每个 Controller 的 WebAPI 上强制 SnakeCase 反序列化

c# - 如何序列化第 3 方类型以跨 AppDomain 进行通信?

azure - Web App 部署后未加载

azure - 使用副本的 Microsoft.Web/sites/hostNameBindings 资源的 ARM 模板部署

azure - 使用 Mongodb 和逻辑应用程序插入文档时出现问题