【问题标题】:Query for entire JSON document in nested JSON schema在嵌套 JSON 模式中查询整个 JSON 文档
【发布时间】:2020-12-08 07:01:54
【问题描述】:

背景:

我希望找到具有 "state" = "new" 且 length(Features.id) > 4 条件的整个 JSON 文档

{
    "id": "123"
    "feedback": {
        "Features": [
            {
                "state": "new"
                "id": "12345"

            }

        ]
    }
}

这是我试图做的:

因为这是一个嵌套文档。我的查询如下所示:

stackoverflow 成员帮助我访问了查询中的嵌套内容,但有没有办法获取完整文档

我用过:

  SELECT VALUE t.id FROM t IN f.feedback.Features where t.state = 'new' and length(t.id)>4

这会给我ID。

我的愿望是在这种情况下访问完整的文档?

{
    "id": "123"
    "feedback": {
        "Features": [
            {
                "state": "new"
                "id": "12345"

            }

        ]
    }
}

感谢任何帮助

【问题讨论】:

  • 尝试在选择查询中返回f 变量。
  • 这样吗? SELECT VALUE f.id FROM t IN f.feedback.Features where t.state = 'new' and length(t.id)>4
  • 如果您想要再次获取整个数据,就应该这样做。 SELECT VALUE f FROM t IN f.feedback.Features where t.state = 'new' ...

标签: sql arrays json azure azure-cosmosdb


【解决方案1】:

试试这个

SELECT * 
FROM f 
WHERE 
    f.feedback.Features[0].state = 'new' 
    AND length(f.feedback.Features[0].id)>4

这里是 CosmosDB 的 SELECT 规范以了解更多详细信息

https://docs.microsoft.com/en-us/azure/cosmos-db/sql-query-select

另外,请查看 CosmosDB 注释中的“使用 JSON”

https://docs.microsoft.com/en-us/azure/cosmos-db/sql-query-working-with-json

如果Features 数组有超过1 个值,您可以使用EXISTS 子句在其中搜索。请在此处查看 EXISTS 的规范并附上示例:

https://docs.microsoft.com/en-us/azure/cosmos-db/sql-query-subquery#exists-expression

【讨论】:

  • 好的,谢谢。这对我不起作用。也许它不起作用,因为“功能”是一个数组?
猜你喜欢
  • 1970-01-01
  • 2015-02-05
  • 1970-01-01
  • 2018-03-09
  • 2019-11-11
  • 2014-09-16
  • 1970-01-01
  • 1970-01-01
  • 2023-01-10
相关资源
最近更新 更多