如何在ArangoDB中将两个返回变量的输出合并为一个?

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

ArangoDB初学者请访问:
我有两个输出存储在两个变量中,例如:ab中的一个或多个。
变量ab中的项目结构完全相同,但数据不同。示例如下:

a = {
    "user": "Thor Odinson",
    "city": "New York",
    "action": "Lives"
}

b = {
    "user": "Thor Odinson",
    "city": "New York",
    "action": "Childhood"
}

如何将两个变量的输出合并为一个变量,如下所示?

{
    "user": "Thor Odinson",
    "city": "New York",
    "action": ["Lives", "Childhood"]
}

理想情况下,将用户和城市作为公共分母合并这两个文档,并将操作合并到一个数组中?不确定Arango本身是否有这样的功能,但任何朝着正确方向的帮助都将是一个很大的帮助!
我愿意在代码中编写逻辑,但我希望尽可能避免这种情况。
我一直在玩COLLECTUNIONMERGE,但运气不好。

b4lqfgs4

b4lqfgs41#

data定义为

LET a = {
    "user": "Thor Odinson",
    "city": "New York",
    "action": "Lives"
}

LET b = {
    "user": "Thor Odinson",
    "city": "New York",
    "action": "Childhood"
}

LET data = [a,b]

要获得data的所需结果,应该可以从以下内容开始:

FOR i IN data
    COLLECT user = i.user, city = i.city INTO groups = i.action 
    RETURN {"user": user, "city": city, "action": groups}

这样就得到了所需的结果:

[
  {
    "user": "Thor Odinson",
    "city": "New York",
    "action": [
      "Lives",
      "Childhood"
    ]
  }
]

如果需要对返回的变量进行更多控制,请使用KEEP

FOR i IN data
    LET action = i.action
    COLLECT user = i.user, city = i.city INTO groups KEEP action
    RETURN {"user": user, "city": city, "action": groups[*].action}

相关问题