数组的ArangoDB数组查询

hm2xizp9  于 2022-12-09  发布在  Go
关注(0)|答案(1)|浏览(153)

我正在尝试在arangodb中编写一个更新命令,以便在具有嵌套数组的文档中插入一个“key”:“value”属性。

{
  "OuterBlock": {
    "InnerBlock": {
      "key1": "value1",
      "key2": {
        "key21": "value21"
      },
      "key3": {
        "key31": "value31"
      },
      "key4": [
        {
          "key41": "value1",
          "key42": {
            "key421": "value421"
          },
          "key43": [
            {
              "key431": "value431",
              "key432": {
                "key4321": "value4321"
              }
            }
          ]
        },
        {
          "key44": "value44",
          "key45": {
            "key451": "key451"
          }
        }
      ]
    }
  }
}

我需要在key432下再添加一个key:value对(例如:“key4322”:“value4322”)。我首先尝试使用选择查询,然后尝试使用MERGE命令添加此属性。

FOR t IN test 
FILTER t._key=="Test"
Collect a = t.OuterBlock.InnerBlock.key4[0].key43[0].key432 into aitems
LET newa = (MERGE(a , {"key4322": "value4322"}))
RETURN newa

返回结果如下

[
  {
    "key4321": "value4321",
    "key4322": "value4322"
  }
]

所以我试着用下面的查询将这个结果与第一个块“key43”合并

FOR t IN test
FILTER t._key=="Test"
collect a = t.OuterBlock.InnerBlock.key4[0].key43[0]  into aitems
LET newa = (MERGE(a , {key432: 
(
FOR t IN test 
FILTER t._key=="Test"
Collect b = t.OuterBlock.InnerBlock.key4[0].key43[0].key432 into bitems
LET newb = (MERGE(b , {"key4322": "value4322"}))
Return newb
)
}))
RETURN newa

输出在key432中给了我一个额外的数组块[],它在原始数据中是不存在的。因此它改变了文档的格式。我该如何删除这个数组块。请建议。

[
  {
    "key431": "value431",
    "key432": **[**
      {
        "key4321": "value4321",
        "key4322": "value4322"
      }
    **]**
  }
]
6za6bjd0

6za6bjd01#

您需要逐步替换数组元素和合并对象,因为AQL中的变量是不可变的。在客户端扩展嵌套对象,然后在服务器端替换整个文档会更容易。尽管如此,在AQL中还是可以这样做:

FOR t IN test
  FILTER t._key == "test"
  LET key432 = MERGE(
    t.OuterBlock.InnerBlock.key4[0].key43[0].key432,
    { key4322: "value4322" }
  )
  LET key43 = REPLACE_NTH(
    t.OuterBlock.InnerBlock.key4[0].key43, 0,
    MERGE(t.OuterBlock.InnerBlock.key4[0].key43[0], { key432 })
  )
  LET key4 = REPLACE_NTH(t.OuterBlock.InnerBlock.key4, 0,
    MERGE(t.OuterBlock.InnerBlock.key4[0], { key43 })
  )
  RETURN MERGE_RECURSIVE(t, { OuterBlock: { InnerBlock: { key4 } } })

结果:

[
  {
    "OuterBlock": {
      "InnerBlock": {
        "key1": "value1",
        "key2": {
          "key21": "value21"
        },
        "key3": {
          "key31": "value31"
        },
        "key4": [
          {
            "key41": "value1",
            "key42": {
              "key421": "value421"
            },
            "key43": [
              {
                "key431": "value431",
                "key432": {
                  "key4321": "value4321",
                  "key4322": "value4322"
                }
              }
            ]
          },
          {
            "key44": "value44",
            "key45": {
              "key451": "key451"
            }
          }
        ]
      }
    }
  }
]

关于额外的数组,请记住,子查询总是返回一个数组,就像顶级查询一样。

LET newa = (MERGE(a , {key432: 
(
FOR t IN test ... // subquery
)

若要只获取子查询返回的第一个元素,可以执行FIRST( FOR ... )( FOR ... )[0]

相关问题