简体   繁体   English

如何 SQL 在 MS Azure CosmosDB 中查询嵌套列表和数组?

[英]How to SQL query a nested list and array in a MS Azure CosmosDB?

I have the following ( Azure ) CosmosDB (sub) structure, that has 2 nested arrays:我有以下 ( Azure ) CosmosDB (子)结构,它有 2 个嵌套的 arrays:

{
    "id": "documentTypes",
    "SomeThing": "SomeThing",
    "configuration": [
        {
            "language": "en",
            "list": [
                {
                    "id": 1,
                    "name": "Supporting Documents"
                },
                {
                    "id": 2,
                    "name": "Summary PDF"
                },
            ]
        }
    ],
}

I have tried the following queries, with poor results.我尝试了以下查询,但结果很差。

SELECT * FROM c WHERE c.documentTypes.configuration[0].list[0].id FROM c

and

SELECT
    p.id,       
    p.name
FROM f
    JOIN c IN f.configuration
    JOIN p IN c.list
WHERE c.id == 'documentTypes'

Q : How can I get only the list of name and id s? Q :我怎样才能只得到nameid的列表?

You need this?你需要这个?

SELECT ARRAY(SELECT VALUE e FROM c JOIN d IN c["configuration"] JOIN e IN d["list"]) AS Result FROM c

Output: Output:

[ { "Result": [ { "id": 1, "name": "Supporting Documents" }, { "id": 2, "name": "Summary PDF" } ] } ] [ { "Result": [ { "id": 1, "name": "Supporting Documents" }, { "id": 2, "name": "Summary PDF" } ] } ]

My own solution was similar to Sajeetharan 's:我自己的解决方案类似于Sajeetharan的:

SELECT list.id, list.name FROM c
    JOIN configuration IN c.configuration 
    JOIN list IN configuration.list
WHERE c.id = 'documentTypes'

This to me look a bit simpler by not needing [""] and the ARRAY() function, and also doesn't produce the additional Result item.这对我来说看起来更简单一些,因为不需要[""]ARRAY() function,而且不会产生额外的Result项。 I have not idea if there is any performance difference.我不知道是否有任何性能差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM