如何使用Cosmos DB查询嵌套数组

时间:2019-07-10 19:05:31

标签: azure-cosmosdb

给出以下JSON

{
"Families": [
    { "Name": "Smith",
      "Children": [
        { "Name": "Bob",
            "Pets": [
              { "Name": "Oscar", "Type": "Cat"},
              { "Name": "Otto", "Type": "Dog"}
            ]
        },
        { "Name": "Brittney",
            "Pets": [
              { "Name": "Isolde", "Type": "Dog"},
              { "Name": "Ignatz", "Type": "Turtle"}
            ]
        }
    ]
    },
    { "Name": "Miller",
      "Children": [
        { "Name": "Alex",
            "Pets": [
              { "Name": "Elvis", "Type": "Horse"}
            ]
        }
    ]
    }
]
}

A)我想创建一个产生以下结构的查询

[
    { "FamilyName": "Smith",
    "KidName": "Bob",
    "Petname": "Oscar"
    },
    { "FamilyName": "Smith",
    "KidName": "Bob",
    "Petname": "Otto"
    },
    { "FamilyName": "Smith",
    "KidName": "Brittney",
    "Petname": "Isolde"
    },
    { "FamilyName": "Smith",
    "KidName": "Brittney",
    "Petname": "Ignatz"
    },
    { "FamilyName": "Miller",
    "KidName": "Alex",
    "Petname": "Elvis"
    }
]

B)我想创建一个查询,该查询产生的结构略有不同

[
    { "FamilyName": "Smith",
    "KidName": "Bob",
    "Petnames": ["Oscar", Otto"]
    },
    { "FamilyName": "Smith",
    "KidName": "Brittney",
    "Petname": ["Isolde", "Ignatz"]
    },
    { "FamilyName": "Miller",
    "KidName": "Alex",
    "Petname": ["Elvis"]
    }
]

非常感谢您的帮助

罗伯特

1 个答案:

答案 0 :(得分:2)

实际上,这里有一个非常好的文档:https://docs.microsoft.com/en-us/azure/cosmos-db/sql-query-join和此处:https://docs.microsoft.com/en-us/azure/cosmos-db/sql-query-object-array#arrays

对于数组,您想使用嵌套联接,如下所示:

给出:

{
    "id": "so-test",
    "Families": [
        {
            "Name": "Smith",
            "Children": [
                {
                    "Name": "Bob",
                    "Pets": [
                        {
                            "Name": "Oscar",
                            "Type": "Cat"
                        },
                        {
                            "Name": "Otto",
                            "Type": "Dog"
                        }
                    ]
                },
                {
                    "Name": "Brittney",
                    "Pets": [
                        {
                            "Name": "Isolde",
                            "Type": "Dog"
                        },
                        {
                            "Name": "Ignatz",
                            "Type": "Turtle"
                        }
                    ]
                }
            ]
        },
        {
            "Name": "Miller",
            "Children": [
                {
                    "Name": "Alex",
                    "Pets": [
                        {
                            "Name": "Elvis",
                            "Type": "Horse"
                        }
                    ]
                }
            ]
        }
    ]
}

您可以将此查询用于A:

SELECT 
    f.Name as FamilyName,
    c.Name as KidName,
    p.Name as Petname
FROM d
JOIN f IN d.Families
JOIN c IN f.Children
JOIN p IN c.Pets
WHERE d.id = "so-test"

您将得到以下结果:

[
    {
        "FamilyName": "Smith",
        "KidName": "Bob",
        "Petname": "Oscar"
    },
    {
        "FamilyName": "Smith",
        "KidName": "Bob",
        "Petname": "Otto"
    },
    {
        "FamilyName": "Smith",
        "KidName": "Brittney",
        "Petname": "Isolde"
    },
    {
        "FamilyName": "Smith",
        "KidName": "Brittney",
        "Petname": "Ignatz"
    },
    {
        "FamilyName": "Miller",
        "KidName": "Alex",
        "Petname": "Elvis"
    }
]

对于B),请使用此查询,其中添加了ARRAY方法

SELECT 
    f.Name as FamilyName,
    c.Name as KidName,
    ARRAY(SELECT DISTINCT VALUE p.Name from p IN c.Pets) as Petname
FROM d
JOIN f IN d.Families
JOIN c IN f.Children
WHERE d.id = "so-test"

您将获得以下结果:

[
    {
        "FamilyName": "Smith",
        "KidName": "Bob",
        "Petname": [
            "Oscar",
            "Otto"
        ]
    },
    {
        "FamilyName": "Smith",
        "KidName": "Brittney",
        "Petname": [
            "Isolde",
            "Ignatz"
        ]
    },
    {
        "FamilyName": "Miller",
        "KidName": "Alex",
        "Petname": [
            "Elvis"
        ]
    }
]