【问题标题】:Return the content of a specific object in an array — CosmosDB返回数组中特定对象的内容 — CosmosDB
【发布时间】:2019-05-15 09:28:00
【问题描述】:

这是对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"
        }
    ]
}
]

问题

如何更改我的查询,以便我选择 only 包含“名称”“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) 错误

【问题讨论】:

  • 您好像发过两次这个问题,第一次是here。很好奇为什么。请不要重复同样的问题。
  • 这个问题(Return the content of a specific object in an array — CosmosDB)与您提到的原始问题有点不同,因此认为发布后续问题更合适。不建议在评论字段中发布后续问题(就像我在原始帖子中所做的那样)。
  • 看起来完全一样的根本原因:在查询中使用保留字(两个答案都指出)。

标签: azure-cosmosdb azure-cosmosdb-sqlapi


【解决方案1】:

你的想法和方向绝对正确,我简化并测试了你的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" })

发现错误信息如下:

因为value是cosmos db sql语法中的保留字,请参考这个案例:Using reserved word field name in DocumentDB

你可以尝试修改sql:

SELECT detail["value"]  FROM c 

【讨论】:

  • 这应该被标记为您提到的问题(和答案)的副本。实际上,将保留关键字作为属性名称处理的问题(和答案)不止一个,上下文略有不同。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-11
  • 2022-01-01
  • 1970-01-01
  • 2021-07-28
  • 2021-10-17
  • 2016-09-16
  • 2020-03-24
相关资源
最近更新 更多