所以,让我们说我收集了用户和公司。然后我有一个名为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"
}
]
}
]
然而,我想得到"用户"信息没有嵌套在"用户"但如下:
[
{
"_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")
那么,有没有人知道我怎么能"不需要"用户好吗?谢谢
答案 0 :(得分:1)
您可以使用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})