检查数组的内容 -- CosmosDB
Check the content of an array -- CosmosDB
我使用以下查询查询 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" 为真时我才能得到响应。
我天真地尝试过但没有成功:
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) 错误消息。
任何 direction/help 来实现这个?
Value
关键字有问题。 Cosmos SQL 以多种不同的方式使用 Value Keyword,这可能是一个原因,我们不能在 select 查询中使用值字段。
我用 value1
而不是 value
更改了文档,那么您的查询有效。
建议
如果您在数组中应用过滤器,请始终使用 Array_Contains。如果数组 EndDeviceEventDetail
中的值顺序发生变化,您的查询将不会 return 正确的结果。
我的查询
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 数据库:
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" 为真时我才能得到响应。
我天真地尝试过但没有成功:
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) 错误消息。 任何 direction/help 来实现这个?
Value
关键字有问题。 Cosmos SQL 以多种不同的方式使用 Value Keyword,这可能是一个原因,我们不能在 select 查询中使用值字段。
我用 value1
而不是 value
更改了文档,那么您的查询有效。
建议
如果您在数组中应用过滤器,请始终使用 Array_Contains。如果数组 EndDeviceEventDetail
中的值顺序发生变化,您的查询将不会 return 正确的结果。
我的查询
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"
}
]
}
]