Check the content of an array — CosmosDB

…衆ロ難τιáo~ 提交于 2019-12-25 00:10:03

问题


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:


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"
        }
    ]
}
]


来源:https://stackoverflow.com/questions/56126817/check-the-content-of-an-array-cosmosdb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!