【问题标题】:DocumentDB Sub QueryDocumentDB 子查询
【发布时间】:2019-06-18 01:55:42
【问题描述】:

我正在尝试从包含双嵌套数组的大型文档投影到数组的扁平表示中,但我一直不知道如何继续。

我有类似的文件:

{
    "id": "1",
    "themeId": "e4d3549c-2785-4067-83d6-f396d2212776",
    "enabled": false,
    "archived": false,
    "componentGroups": [
      [
        {
          "componentType": "header",
          "enabled": true,
          "configurationVariables": {
            "text1": "AAA",
            "text2": "BBB"
          }
        }
      ],
      [
        {
          "componentType": "prompt",
          "enabled": true,
          "configurationVariables": {
            "text1": "AAA",
            "text2": "BBB"
          }
        },
        {
          "componentType": "proactive",
          "enabled": true,
          "configurationVariables": {
            "text1": "AAA",
            "text2": "BBB"
          }
        }
      ],
      [
        {
          "componentType": "product-feed",
          "enabled": true,
          "configurationVariables": {
            "text1": "AAA",
            "text2": "BBB"
          }
        }
      ]
    ]
  }

我正在尝试将其投影到以下结构:

{
    "id": "275973",
    "themeId": "e4d3549c-2785-4067-83d6-f396d2212776",
    "enabled": false,
    "archived": false,
    "components": [
        {
          "componentType": "header",
          "enabled": true
        },
        {
          "componentType": "prompt",
          "enabled": true,
        },
        {
          "componentType": "proactive",
          "enabled": true,
        },
        {
          "componentType": "product-feed",
          "enabled": true
        }
      ]
    ]
  }

我使用以下查询取得了一些成功:

SELECT T.id, 
    T.themeId, 
    T.enabled, 
    T.archived, 
    [ { type: C.componentType, enabled: C.enabled } ] AS components
FROM Panels T 
JOIN CG IN T.componentGroups
JOIN C IN CG
WHERE T.id IN ("275973")

但是,这会为每个组件类型返回一个单独的记录。我试图将它们全部折叠在一起,以便所有组件都在包含文档的单个实例中。我希望能够做一些类似嵌套 SELECT 的事情,我可以在其中加入外部文档,类似于:

SELECT T.id, 
T.themeId,
T.enabled, 
T.archived, 
[ 
    SELECT C.componentType, C.enabled
    FROM CG IN T.componentGroups
    JOIN C IN CG 
] AS components
FROM Panels T
WHERE T.id IN ("275973")

但这是无效的。我正在寻找有关子/嵌套选择的信息,并通过钻入嵌套数组来返回数据。

【问题讨论】:

  • 为了确保我理解这个问题,您想从子元素中删除 configurationVariables 字段并展平组件列表,对吗?此外,您的预期输出中还有许多其他字段未出现在文档示例中。那些已经处于最高水平了吗?我打算提供的答案是一个 UDF,它将为您进行“投影”。我怀疑您的建议可以在纯 SQL 中完成,但我发现 JavaScript UDF 比使用 SQL 更容易完成这些转换。
  • 嗨拉里。更正了投影文档。是的,你在我想要实现的目标上是正确的。我目前正在尝试避免 UDF,因为我没有一个好的策略来管理它们,因为我希望它可以通过在应用程序启动时同步但应用程序部署到大量机器上,所以我可以看到这会导致问题。
  • 我的系统中有一个主节点,每次启动时都会重新加载所有 Sproc 和 UDF。我也可以随时触发一些事情来做这件事。但是,如果您真的想避免 UDF,那么我不知道该怎么做。其他人可能会。我见过一些 SQL 高手回答这些问题。

标签: azure-cosmosdb


【解决方案1】:

已计划对子查询的 DocumentDB 支持,但目前不支持。同时,UDF 或将数据客户端拉为 N 条记录,然后重新格式化是当今最好的方法。对于其他感兴趣的人,这是一个用于在查询中返回结果的 UDF,

function transform(doc) {
    var result = {};

    for (var prop in doc) {
        if (prop != "componentGroups") {
            result[prop] = doc[prop];
        } else {
            result["components"] = [];
            for(var cgidx in doc["componentGroups"]) {
                var componentGroup = doc["componentGroups"][cgidx];
                for (var cidx in componentGroup) {
                    var component = componentGroup[cidx];
                    result["components"].push({componentType: component.componentType, enabled: component.enabled });
                }
            }
        }
    }

    return result;
 }

【讨论】:

  • 您好,有关于子查询支持的消息吗?
  • 是的子查询现在可用(但尚未记录!)
  • 我通过电子邮件向 askcosmosdb 发送了一个问题 Aravind (stackoverflow.com/questions/51042600/…),我认为子查询可能会解决(投影的子查询)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-26
  • 1970-01-01
  • 2018-03-08
  • 1970-01-01
  • 2015-09-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多