我尝试使用在第一个文档上找到的一些属性一次查询多个文档,类似于属性值上的 TSQL 左联接。
我在 CosmosDB 中的尝试:
select c from assets
join ver on c.versions
where c.id = '123' OR c.id IN ver.otherIds
--NOTE: ver.otherIds is an array
上面的查询导致语法错误,指出它不理解ver.otherIds
。文档说明了要where c.id in ("123","456"...)
的语法
我试图解决这个问题的事情:
- 尝试接收数组的自定义 UDF 会生成所需的语法 Ex)
["123,"456"] --> "("123", "456")
- 尝试使用
array_contains(ver.otherIds, c.id)
尝试的子查询 - 方法,该方法生成"标量子查询结果集的基数不能大于 1"错误:
select value c from c
where array_contains((select ... that produces array), c.id)
以上都不起作用。
当然,我可以拉取第一个资产,然后生成第二个查询来拉取其余资产,但我宁愿不这样做。我也可以对所有数据进行非规范化,但如果不详细说明我的方案,最终将是一个非常糟糕的主意。
有什么想法吗?
提前感谢!
您可以使用第二个场景:ARRAY_CONTAINS。
我的示例文档:
[
{
"id": "1",
"versions": [
{
"otherIds": [
"1",
"2",
"3"
]
}
]
},
{
"id": "2",
"versions": [
{
"otherIds": [
"1",
"2",
"3"
]
},
{
"otherIds": [
"123",
"2",
"3"
]
}
]
},
{
"id": "123",
"versions": [
{
"otherIds": [
"1",
"2",
"3"
]
},
{
"otherIds": [
"123",
"2",
"3"
]
}
]
}
]
.SQL:
SELECT distinct c.id,c.versions FROM c
join ver in c.versions
where c.id="123" or array_contains(ver.otherIds,c.id,false)
ARRAY_CONTAINS函数可以指定匹配是完全匹配还是部分匹配。