"unnest" arangodb中的aql查询结果
"unnest" aql query result in arangodb
假设我收集了用户和公司。然后我有一个名为 works_in 的边缘集合,它将用户与公司联系起来。
我正在使用以下 aql 查询:
FOR user IN Users
LET companies = (FOR company IN (NEIGHBORS(Users, works_in, user._id, 'outbound', [], {includeData:true}))
RETURN {company_name: company.name, company_id: company._id})
RETURN {user, companies}
我得到的是:
[
{
"user": {
"_id": "Users/45645",
"_key": "45645",
"_rev": "45645",
"name": "user1",
"city": "london",
"age": 23
},
"companies": [
{
company_name: "company1",
company_id: "Companies/7897"
},
{
company_name: "company2",
company_id: "Companies/7878"
}
]
},
{
"user": {
"_id": "Users/465454",
"_key": "465454",
"_rev": "465454",
"name": "user2",
"city": "Paris",
"age": 42
},
"companies": [
{
company_name: "company1",
company_id: "Companies/7897"
},
{
company_name: "company3",
company_id: "Companies/788233"
}
]
}
]
但是我想获得 "users" 信息,而不是嵌套在 "user" 中,而是如下所示:
[
{
"_id": "Users/45645",
"_key": "45645",
"_rev": "45645",
"name": "user1",
"city": "london",
"age": 23,
"companies": [
{
company_name: "company1",
company_id: "Companies/7897"
},
{
company_name: "company2",
company_id: "Companies/7878"
}
]
},
{
"_id": "Users/465454",
"_key": "465454",
"_rev": "465454",
"name": "user2",
"city": "Paris",
"age": 42,
"companies": [
{
company_name: "company1",
company_id: "Companies/7897"
},
{
company_name: "company3",
company_id: "Companies/788233"
}
]
}
]
我知道我可以通过某种硬编码属性来实现它,例如
RETURN {_id: user.id, _key:user._key, companies}
但问题是我有很多属性要显示,而且用户可能没有某些特定属性(因此显示为 "null")
所以,有谁知道我怎样才能正确地 "unnest" 用户?谢谢
您可以使用 MERGE。
让我用 return 语句来证明这一点:
db._query(`RETURN MERGE({
"_id": "Users/465454", "_key": "465454",
"_rev": "465454", "name": "user2",
"city": "Paris", "age": 42
}, {"companies": [
{
company_name: "company1",
company_id: "Companies/7897"
},
{
company_name: "company3",
company_id: "Companies/788233"
}
]})`
).toArray() =>
[
{
"_id" : "Users/465454",
"_key" : "465454",
"_rev" : "465454",
"age" : 42,
"city" : "Paris",
"companies" : [
{
"company_id" : "Companies/7897",
"company_name" : "company1"
},
{
"company_id" : "Companies/788233",
"company_name" : "company3"
}
],
"name" : "user2"
}
]
既然公司给了你一个列表,你需要把它包装成一个对象,这样 MERGE
才能完成它的工作:
FOR user IN Users
LET companiesList = (FOR company IN
(NEIGHBORS(Users, works_in, user._id, 'outbound', [],
{includeData:true}))
RETURN MERGE(user, {companies: companiesList})
假设我收集了用户和公司。然后我有一个名为 works_in 的边缘集合,它将用户与公司联系起来。 我正在使用以下 aql 查询:
FOR user IN Users
LET companies = (FOR company IN (NEIGHBORS(Users, works_in, user._id, 'outbound', [], {includeData:true}))
RETURN {company_name: company.name, company_id: company._id})
RETURN {user, companies}
我得到的是:
[
{
"user": {
"_id": "Users/45645",
"_key": "45645",
"_rev": "45645",
"name": "user1",
"city": "london",
"age": 23
},
"companies": [
{
company_name: "company1",
company_id: "Companies/7897"
},
{
company_name: "company2",
company_id: "Companies/7878"
}
]
},
{
"user": {
"_id": "Users/465454",
"_key": "465454",
"_rev": "465454",
"name": "user2",
"city": "Paris",
"age": 42
},
"companies": [
{
company_name: "company1",
company_id: "Companies/7897"
},
{
company_name: "company3",
company_id: "Companies/788233"
}
]
}
]
但是我想获得 "users" 信息,而不是嵌套在 "user" 中,而是如下所示:
[
{
"_id": "Users/45645",
"_key": "45645",
"_rev": "45645",
"name": "user1",
"city": "london",
"age": 23,
"companies": [
{
company_name: "company1",
company_id: "Companies/7897"
},
{
company_name: "company2",
company_id: "Companies/7878"
}
]
},
{
"_id": "Users/465454",
"_key": "465454",
"_rev": "465454",
"name": "user2",
"city": "Paris",
"age": 42,
"companies": [
{
company_name: "company1",
company_id: "Companies/7897"
},
{
company_name: "company3",
company_id: "Companies/788233"
}
]
}
]
我知道我可以通过某种硬编码属性来实现它,例如
RETURN {_id: user.id, _key:user._key, companies}
但问题是我有很多属性要显示,而且用户可能没有某些特定属性(因此显示为 "null")
所以,有谁知道我怎样才能正确地 "unnest" 用户?谢谢
您可以使用 MERGE。 让我用 return 语句来证明这一点:
db._query(`RETURN MERGE({
"_id": "Users/465454", "_key": "465454",
"_rev": "465454", "name": "user2",
"city": "Paris", "age": 42
}, {"companies": [
{
company_name: "company1",
company_id: "Companies/7897"
},
{
company_name: "company3",
company_id: "Companies/788233"
}
]})`
).toArray() =>
[
{
"_id" : "Users/465454",
"_key" : "465454",
"_rev" : "465454",
"age" : 42,
"city" : "Paris",
"companies" : [
{
"company_id" : "Companies/7897",
"company_name" : "company1"
},
{
"company_id" : "Companies/788233",
"company_name" : "company3"
}
],
"name" : "user2"
}
]
既然公司给了你一个列表,你需要把它包装成一个对象,这样 MERGE
才能完成它的工作:
FOR user IN Users
LET companiesList = (FOR company IN
(NEIGHBORS(Users, works_in, user._id, 'outbound', [],
{includeData:true}))
RETURN MERGE(user, {companies: companiesList})