检查数组的内容-CosmosDB [英] Check the content of an array -- CosmosDB
问题描述
我使用以下查询查询CosmosDB数据库:
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')
我得到回应
[
{
"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时得到响应.
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'
但是我收到BadRequest(400)错误消息. 有什么方向/帮助实现这一目标吗?
but I get a BadRequest (400) error message. Any direction/help to achieve this ?
推荐答案
Value
关键字存在问题. Cosmos SQL使用值关键字有许多不同的原因,这可能是一个原因,我们不能在选择查询中使用值字段.
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.
我用value1
而不是value
更改了文档,然后您的查询起作用了.
I Changed the document with value1
instead value
then your query is working.
建议
如果要在数组中应用过滤器,请始终使用 Array_Contains .如果数组EndDeviceEventDetail
中的值顺序发生变化,则查询将不会返回正确的结果.
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.
我的查询
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"
}
]
}
]
这篇关于检查数组的内容-CosmosDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!