Check the content of an array — CosmosDB

2019-08-17 02:24发布

I query a CosmosDB database using the following query :

SELECT c.EventType.EndDeviceEventDetail FROM c 
WHERE (c.EventType.EndDeviceEventType.eventOrAction = '93'
AND c.EventType.EndDeviceEventType.subdomain = '137'
AND c.EventType.EndDeviceEventType.domain = '26'
AND c.EventType.EndDeviceEventType.type = '3')

I get as a response

[
    {
        "EndDeviceEventDetail": [
            {
                "name": "Spontaneous",
                "value": "true"
            },
            {
                "name": "DetectionActive",
                "value": "true"
            },
            {
                "name": "RCDSwitchReleased",
                "value": "false"
            }
        ]
    },
    {
        "EndDeviceEventDetail": [
            {
                "name": "Spontaneous",
                "value": "true"
            },
            {
                "name": "DetectionActive",
                "value": "true"
            },
            {
                "name": "RCDSwitchReleased",
                "value": "true"
            }
        ]
    }
]

I would like to get as step further and modify my query so that I get a response only if "RCDSwitchReleased" is true.

I naïvely tried without success :

SELECT c.EventType.EndDeviceEventDetail FROM c 
WHERE (c.EventType.EndDeviceEventType.eventOrAction = '93'
AND c.EventType.EndDeviceEventType.subdomain = '137'
AND c.EventType.EndDeviceEventType.domain = '26'
AND c.EventType.EndDeviceEventType.type = '3')
AND c.EventType.EndDeviceEventDetail[2].value = 'true'

but I get a BadRequest (400) error message. Any direction/help to achieve this ?

1条回答
对你真心纯属浪费
2楼-- · 2019-08-17 02:51

An issue with Value Keyword. Cosmos SQL use Value Keyword in many different ways, this is might be a reason, we can't use value field in select query.

I Changed the document with value1 instead value then your query is working.

Suggestion

If you are applying a filter in the array, Always use Array_Contains. If order of value inside your array EndDeviceEventDetail would change, your query will not return the correct result.

My query

SELECT c.EventType.EndDeviceEventDetail FROM c 
WHERE c.EventType.EndDeviceEventType.eventOrAction = '93'
AND c.EventType.EndDeviceEventType.subdomain = '137'
AND c.EventType.EndDeviceEventType.domain = '26'
AND c.EventType.EndDeviceEventType.type = '3'
AND ARRAY_CONTAINS(c.EventType.EndDeviceEventDetail,{"name": 
"RCDSwitchReleased","value1": "true" })

Query Output

[
{
    "EndDeviceEventDetail": [
        {
            "name": "Spontaneous",
            "value1": "true"
        },
        {
            "name": "DetectionActive",
            "value1": "true"
        },
        {
            "name": "RCDSwitchReleased",
            "value1": "true"
        }
    ]
}
]
查看更多
登录 后发表回答