【问题标题】:Cosmos DB query syntax WHERE clause with array in arrayCosmos DB 查询语法 WHERE 子句与数组中的数组
【发布时间】:2019-12-04 09:48:24
【问题描述】:

以下 json 表示 Cosmos DB 容器中的两个文档。 如何编写一个查询来获取具有item 的任何文档,其中iditem_1,值为bar

我已经研究了ARRAY_CONTAINS,但不要让它与数组中的数组一起使用。 我也尝试过any。虽然我似乎找不到任何关于如何使用它的文档,但any 似乎是一个有效的函数,因为我在 Azure 门户的 cosmos db 资源管理器中得到了formatting highlights。 对于any 函数,我尝试了SELECT * FROM c WHERE c.pages.any(p, p.items.any(i, i.id = "item_1" AND i.value = "bar")) 之类的方法。

id 字段是唯一的,因此如果更容易找到包含具有正确 id 和值的任何对象的任何文档,那也很好。

[
  {
    "type": "form",
    "id": "form_a",
    "pages": [
      {
        "name": "Page 1",
        "id": "page_1",
        "items": [
          {
            "id": "item_1",
            "value": "foo"
          }
        ]
      }
    ]
  },
  {
    "type": "form",
    "id": "form_b",
    "pages": [
      {
        "name": "Page 1",
        "id": "page_1",
        "items": [
          {
            "id": "item_1",
            "value": "bar"
          }
        ]
      }
    ]
  }
]

【问题讨论】:

    标签: azure-cosmosdb


    【解决方案1】:

    我认为join可以处理WHERE clause with array in array。请在sql下面测试:

    SELECT c.id FROM c
    join pages in c.pages
    where array_contains(pages.items,{"id": "item_1","value": "bar"},true)
    

    输出:

    【讨论】:

    • 谢谢,这很有帮助。有没有机会得到 (SELECT) 整个文档 bij 做类似SELECT c.* FROM c join pages in c.pages ... 的事情?
    • @markbeij 好吧,您不能将c.*join 一起使用。但是,您可以使用select c.A,c.B,c.C from ..... 来覆盖所有列。也许有点麻烦,但它应该可以工作。
    • 谢谢。我试图让我的解决方案尽可能通用。
    猜你喜欢
    • 2020-12-01
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 2010-10-24
    • 1970-01-01
    • 2018-10-04
    相关资源
    最近更新 更多