gpt4 book ai didi

azure - 查询 CosmosDb 非结构化 JSON

转载 作者:行者123 更新时间:2023-12-05 06:30:21 25 4
gpt4 key购买 nike

CosmosDB 如何查询动态 JSON 中的属性值?

该应用程序允许将 JSON 存储为对象的一组自定义属性。它们被序列化并存储在 CosmosDb 中。例如,这里有两个条目:

{
"id": "ade9f2d6-fff6-4993-8473-a2af40f071f4",
...
"Properties": {
"fn": "Ernest",
"ln": "Hemingway",
"a_book": "The Old Man and the Sea"
},
...
}

{
"id": "23cb9d4c-da56-40ec-9fbe-7f5178a92a4f",
...
"Properties": {
"First Name": "Salvador",
"Last Name": "Dali",
"Period": "Surrealism"
},
...
}

如何构造查询以便在 Properties 的值中进行搜索?

最佳答案

I’m looking for something that doesn’t involve the name of the sub-propety, like SELECT * FROM c WHERE some_function_here(c.Properties, ‘Ernest’)

也许我明白您的想法,您想通过Properties 的值而不是名称来过滤文档。如果是这样,您可以使用 UDF在 Cosmos 数据库中。

udf 示例:

function query(Properties,filedValue){
for(var k in Properties){
if(Properties[k] == filedValue)
return true;
}
return false;
}

示例查询:

SELECT  c.id FROM c where udf.query(c.Properties,'Ernest')

输出:

enter image description here

<小时/>

这里简单总结一下,Ovi的udf函数如下:

function QueryProperties (Properties, filedValue) {     
for (var k in Properties) {
if (Properties[k] && Properties[k].toString().toUpperCase().includes(filedValue.toString().toUpperCase()))
return true;
return false;
}

关于azure - 查询 CosmosDb 非结构化 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52481606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com