如何在Azure Cosmos DB中定义涉及嵌入式数组中属性的唯一键? [英] How do I define unique keys involving properties in embedded arrays in Azure Cosmos DB?

查看:52
本文介绍了如何在Azure Cosmos DB中定义涉及嵌入式数组中属性的唯一键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Azure Cosmos DB中,支持唯一键.每个唯一键都定义为一组路径,这些路径表示存储的文档中的值.这样的路径的一个示例是/contact/firstName .从官方文档尚不清楚(实际上根本没有提及)这些路径如何通过文档中的嵌入式数组向下应用,或者当路径导航到基数大于1的嵌套文档时如何应用独特的键语义.>

例如,假设我有一个像这样的文档来存储一个用户组和一组成员用户:

  {"id":"ABCD1234","name":管理员",成员":[{"userId":1已启用":true},{"userId":2已启用":true}]} 

我希望组名在整个逻辑分区中是唯一的,所以我添加了一个唯一的键,其路径为/groupName .

我还想确保成员是唯一的,即,相同的 userId 值在给定的组中不会出现多次.天真的,我可能会尝试使用路径/name /members/userId 创建唯一键.但这是行不通的,唯一键似乎无效.

我尝试了一些不同的变化,但是没有一个能达到我期望的效果.

所以我的问题:

  1. 是否可以创建遍历"嵌入式对象数组的唯一键?
  2. 如果是这样,正确的路径语法是什么?
  3. 鉴于唯一键的意思是整个逻辑分区唯一",而不是整个文档唯一",如果我实际上 did 设法定义一个包含属性的唯一键,会发生什么情况嵌入的 members 对象,并试图保存两个都有零成员的不同组?那么,对于这两个组,这些键在逻辑上是否不会被评估为 null undefined ,从而阻止我保存其中一个?

非常感谢您提供任何见解来帮助解决此问题!

解决方案

唯一键不会遍历文档中的数组,这就是为什么它们没有被如此记录的原因.

有关什么是逻辑分区的详细信息,请参见

I want the group name to be unique across the logical partition, so I add a unique key with path /groupName.

I also want to ensure that the members are unique, i.e. that the same userId value does not occur more than once within a given group. Naïvely, I might try creating a unique key with the paths /name and /members/userId. But that doesn't work, the unique key seems to have no effect.

I've tried a few different variations of this, but none of them had the effect I was expecting.

So my questions:

  1. Is it possible to create unique keys that "traverse" into arrays of embedded objects?
  2. If so, what is the correct path syntax for that?
  3. Given that unique keys mean "unique across the whole logical partition" as opposed to "unique across the document", what would happen if I actually did manage to define a unique key involving the properties on the embedded members objects, and tried to save two different groups that both have zero members? Would those keys not then logically evaluate as null or undefined for both group, thereby preventing me from saving one of them?

Thankful for any insights to help clear this up!

解决方案

Unique keys do not traverse into arrays within documents which is why they are not documented as such.

For details on what a logical partition is please see our docs on partitions

If you want uniqueness like what you are describing then create as different documents within a logical partition.

这篇关于如何在Azure Cosmos DB中定义涉及嵌入式数组中属性的唯一键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆