展平 cosmosdb 中的嵌套数组 sql

Flatten nested arrays in cosmosdb sql

我有以下 cosmosdb 文档:

{
  id: "id",
  outer: [
    {
      "inner": [ "a", "b", "c" ]
    },
    {
      "inner": [ "d", "e", "f" ]
    }
  ]
}

我需要创建一个 SQL 请求,它将 return “内部”数组的所有组合值,如下所示:

{
   "allInners": [ "a", "b", "c", "d", "e", "f" ]
}

我能够使用“IN”运算符展开第一个数组级别,但我不确定如何再展开一个级别并处理双重甚至三重嵌套数组。 以下是我聚合这些项目的子查询

SELECT
  ... other stuff.
  ARRAY(SELECT VALUE innerObj.inner FROM innerObj IN c.outer) AS allInners,
  ...
FROM c

请尝试这样的操作sql:

select ARRAY(SELECT VALUE e FROM c join d in c["outer"] join e in d["inner"]) AS allInners from c 

这是我的测试结果:

希望对您有所帮助。:)

我找到了以下解决问题的方法(使用嵌套的“IN”和子查询):

ARRAY(
   SELECT VALUE inner
   FROM inner IN (
      SELECT VALUE outers.inner
      FROM outers IN c.outer
  )
)