WHERE clause on an array in Azure DocumentDb

后端 未结 2 1785
后悔当初
后悔当初 2020-12-14 16:20

In an Azure Documentdb document like this

{
\"id\": \"WakefieldFamily\",
\"parents\": [
    { \"familyName\": \"Wakefield\", \"givenName\": \"Robin\" },
            


        
2条回答
  •  误落风尘
    2020-12-14 17:14

    You should take advantage of DocumentDB's JOIN clause, which operates a bit differently than JOIN in RDBMs (since DocumentDB deals w/ denormlaized data model of schema-free documents).

    To put it simply, you can think of DocumentDB's JOIN as self-joins which can be used to form cross-products between nested JSON objects.

    In the context of querying children whose pets given name is "Goofy", you can try:

    SELECT 
        f.id AS familyName,
        c AS child,
        p.givenName AS petName 
    FROM Families f 
    JOIN c IN f.children 
    JOIN p IN c.pets
    WHERE p.givenName = "Goofy"
    

    Which returns:

    [{
        familyName: WakefieldFamily,
        child: {
            familyName: Merriam,
            givenName: Jesse,
            gender: female,
            grade: 1,
            pets: [{
                givenName: Goofy
            }, {
                givenName: Shadow
            }]
        },
        petName: Goofy
    }]
    

    Reference: http://azure.microsoft.com/en-us/documentation/articles/documentdb-sql-query/

    Edit:

    You can also use the ARRAY_CONTAINS function, which looks something like this:

    SELECT food.id, food.description, food.tags
    FROM food
    WHERE food.id = "09052" or ARRAY_CONTAINS(food.tags.name, "blueberries")
    

提交回复
热议问题