返回数组中特定对象的内容— CosmosDB

时间:2019-05-15 09:28:00

标签: azure-cosmosdb azure-cosmosdb-sqlapi

这是对问题56126817

的跟进

我当前的查询

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","value": "true" })

我的查询输出

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

问题

如何更改查询,以便仅选择包含“名称”“ DetectionActive”的数组的“值”? 背后的想法是过滤一个数组条目上的查询,并获得另一个数组条目的“值”作为输出。从这里阅读,应该使用UDF(在这种情况下不是最好的)和JOIN。

第一次尝试

SELECT t.value FROM c JOIN t in c.EventType.EndDeviceEventDetail 
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","value": "true" })

获取错误请求(400)错误

1 个答案:

答案 0 :(得分:0)

您的想法和方向绝对正确,我简化并测试了您的sql。

SELECT detail.value  FROM c 
join detail in c.EventType.EndDeviceEventDetail
WHERE c.EventType.EndDeviceEventType.eventOrAction = '93'
AND ARRAY_CONTAINS(c.EventType.EndDeviceEventDetail,{"name": 
"RCDSwitchReleased","value": "true" })

发现如下错误消息:

enter image description here

因为value是cosmos db sql语法中的保留字,所以请参考这种情况:Using reserved word field name in DocumentDB

您可以尝试像这样修改sql:

SELECT detail["value"]  FROM c