我使用以下查询查询CosmosDB数据库:
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')
我得到回应
[
{
"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"
}
]
}
]
我想更进一步,修改查询,以便仅在“ RCDSwitchReleased”为true时得到响应。
我天真的尝试没有成功:
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'
但是我收到BadRequest(400)错误消息。 有什么方向/帮助实现这一目标吗?
答案 0 :(得分:0)
Value
关键字存在问题。 Cosmos SQL以多种不同方式使用Value Keyword,这可能是一个原因,我们无法在选择查询中使用值字段。
我用value1
而不是value
更改了文档,然后您的查询开始工作了。
建议
如果要在数组中应用过滤器,请始终使用Array_Contains。如果数组EndDeviceEventDetail
中的值顺序将改变,则查询将不会返回正确的结果。
我的查询
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" })
查询输出
[
{
"EndDeviceEventDetail": [
{
"name": "Spontaneous",
"value1": "true"
},
{
"name": "DetectionActive",
"value1": "true"
},
{
"name": "RCDSwitchReleased",
"value1": "true"
}
]
}
]