azure - 查询 CosmosDb 非结构化 JSON

标签 azure azure-sql-database azure-cosmosdb azure-cosmosdb-sqlapi

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/

相关文章:

azure - 在Microsoft Azure的Data Lake Storage帐户的输出文件夹中写回文件时更改文件名

c# - Azure DocumentDB异步请求问题

azure-sql-database - Python 3.4.1 插入 SQL Azure (pyodbc)

azure-cosmosdb - 您能否分享 Azure Cosmos DB 更改源的租赁集合?

python-3.x - 将参数传递给 Azure Devops 中的 python 脚本

azure - Windows Azure 中的 System.Diagnostics.Trace.WriteLine

azure - 如何仅加载Azure SQL DB中新添加的文件

azure - Terraform Azure - 将现有的 Azure SQL 数据库移至弹性池中

azure - 删除 Cosmos DB 容器是否会消耗 RU?

azure - DocumentDb 模拟器不工作 - 服务不可用