简体   繁体   English

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

[英]How do I combine the output of two return variables into one in ArangoDB?

Beginner with ArangoDB here:这里是 ArangoDB 的初学者:

I have two outputs stored in two variables, say: a and b .我有两个输出存储在两个变量中,例如: ab
The structure of the items in variable a and b are exactly the same, but with different data.变量ab中的项目结构完全相同,但数据不同。 Example as follows:示例如下:

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

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

How would I combine the output from the two variables into one as follows?我如何将 output 从两个变量合并为一个,如下所示?

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

Ideally, combine the two documents with user and city as common denominator, and action merged into an array?理想情况下,将两个文档以用户和城市作为公分母,并将动作合并到一个数组中? Not sure if Arango has a function like that natively, but any help towards the right direction would be a great help too!不确定 Arango 是否有这样的 function,但任何朝着正确方向的帮助也将是一个很大的帮助!
I am open to writing the logic in my code instead, but I'd like to avoid that as much as possible.我愿意在我的代码中编写逻辑,但我想尽可能避免这种情况。

I've been playing around with COLLECT , UNION , and MERGE but with no luck.我一直在玩COLLECTUNIONMERGE但没有运气。

Defining data asdata定义为

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

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

LET data = [a,b]

To get the desired result for data , this should get you started:要获得data所需的结果,这应该可以帮助您入门:

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

That gives the desired result:这给出了预期的结果:

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

If you need more control over the variables returned, use KEEP :如果您需要对返回的变量进行更多控制,请使用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}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM