如何在 ArangoDB 中将两个 return 变量的输出合并为一个?
How do I combine the output of two return variables into one in ArangoDB?
此处为 ArangoDB 初学者:
我有两个输出存储在两个变量中,比如:a
和 b
。
变量a
和b
中的项结构完全一样,只是数据不同。例子如下:
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"]
}
理想情况下,将两个文档以user和city为公分母,action合并成一个数组?不确定 Arango 本身是否具有这样的功能,但任何朝着正确方向的帮助也会有很大的帮助!
我愿意改为在我的代码中编写逻辑,但我想尽可能避免这种情况。
我一直在尝试 COLLECT
、UNION
和 MERGE
,但没有成功。
定义 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}
此处为 ArangoDB 初学者:
我有两个输出存储在两个变量中,比如:a
和 b
。
变量a
和b
中的项结构完全一样,只是数据不同。例子如下:
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"]
}
理想情况下,将两个文档以user和city为公分母,action合并成一个数组?不确定 Arango 本身是否具有这样的功能,但任何朝着正确方向的帮助也会有很大的帮助!
我愿意改为在我的代码中编写逻辑,但我想尽可能避免这种情况。
我一直在尝试 COLLECT
、UNION
和 MERGE
,但没有成功。
定义 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}