【问题标题】:Query CosmosDB nested structure JSON查询 CosmosDB 嵌套结构 JSON
【发布时间】:2019-05-09 21:08:04
【问题描述】:

我正在为我们的一个应用程序使用 CosmosDB SQLAPI。我使用 Azure 存储探索进行查询。我发现很难查询的 JSON 结构。

{
    "countyid": 0,
    "data": [
        [
            {
                "Elements": [
                    {
                        "ID": 11,
                        "V": false,
                        "R": false
                    },
                    {
                        "ID": 16,
                        "V": false,
                        "R": false
                    },
                    {
                        "ID": 3,
                        "V": false,
                        "R": false
                    },
                    {
                        "ID": 5,
                        "V": false,
                        "R": false
                    }
                ]
            },
            {
                "Elements": [
                    {
                        "ID": 486,
                        "V": false,
                        "R": false
                    },
                    {
                        "ID": 492,
                        "V": false,
                        "R": false
                    }
                ]
            }
        ]
    ]
}

我需要获取 Elements[0] ID = 3,5,11,16 和 Elements2 ID = 486,492

CosmosDB Querying JSON edit window

Azure Storage Explorer window trying to query

Query working without where condition

【问题讨论】:

  • 不知道你的实际需要。 I need to get Elements[0] ID = 3,5,11,16 and Elements[1] ID = 486,492。您想按此条件过滤数据吗?数据的最终结构是什么?
  • 我假设您要查询数据的第一个元素数组包含 3,5,11,16 ID,而第二个元素数组包含 486,492 个 ID。我说的对吗?
  • 类似于 SQL 的东西:SELECT c.data.Elements, c.data.Elements.Id FROM c WHERE c.id in ('8a04fc64-904b-4530-98e8-914e951c0cbc') ORDER BY c ._ts DESC 。我期待的最终结果类似于.Elements[0] 3 Elements[0] 5 Elements[0] 6 Elements[0] 11 Elements[1] 486 Elements[1] 492 是的,我想为每个元素数组选择数组 ID。
  • 还是一头雾水...Elements[0] 3 Elements[0] 5 Elements[0] 6 Elements[0] 11 Elements[1] 486 Elements[1] 492 是什么?结果不过滤任何内容。

标签: json azure-storage azure-cosmosdb qsqlquery azure-cosmosdb-sqlapi


【解决方案1】:

SQL

select value array(select value z.ID from z in a.Elements)
from c
join b in c.data
join a in b

结果

[
    [
        11,
        16,
        3,
        5
    ],
    [
        486,
        492
    ]
]

【讨论】:

  • 很棒的 tiwahu,我正在获取您在查询中所述的 ID。但如果我输入 WHERE c.countyid = 0,它会返回空。你也可以帮忙吗?
【解决方案2】:

查询嵌套数组数据时需要使用join,请试试这个sql:

SELECT distinct c.data  from c
join elem1 in c.data[0].Elements
join elem2 in c.data[1].Elements
where elem1.ID in(3,5,6,11) and elem2.ID in (486,492)

【讨论】:

  • Jay,如果你看到这个 JSON“数据”:[[{“元素”......意思是在“数据”字段之后我们有两个方括号,然后是一个花括号,然后是“元素”在这个结构里面。我正在尝试 CosmosDB 查询中不支持的 Select c.data...Elements 事件。甚至对我不起作用的第一件事是获取元素的嵌套查询。提供的查询未返回。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-18
  • 1970-01-01
  • 2018-05-26
  • 2014-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多