查询CosmosDb非结构化JSON [英] Query CosmosDb Unstructured JSON

查看:78
本文介绍了查询CosmosDb非结构化JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can CosmosDB Query the values of the properties within a dynamic JSON?

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

The app allows storing a JSON as a set of custom properties for an object. They are serialized and stored in CosmosDb. For example, here are two entries:

{
    "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的值?

How can the query be structured so that it searches in the values of Properties?

推荐答案

我正在寻找不涉及名称的东西 子属性,例如SELECT * FROM c WHERE some_function_here(c.Properties,'Ernest')

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

Maybe I get your idea that you want to filter the documents by the value of the Properties, not the name. If so , you could use UDF in cosmos db.

示例udf:

sample udf:

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

示例查询:

sample query:

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

输出:

output:

这里只是摘要,Ovi的udf函数如下:

Just summary here, Ovi's udf function like:

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

这篇关于查询CosmosDb非结构化JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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