從@ frant.hartm答案是完全正確的!但是,如果你想用一個稍微不同的方法,你可以考慮以下幾點:
想象一下如下圖:
create
(p1:Employee {name:"John"})-[:works_at]->(e1:Employer {name:"Microsoft"}),
(p1)-[:works_at]->(e2:Employer {name:"Oracle"}),
(p2:Employee {name:"Jim"})-[:works_at]->(e1),
(p2)-[:works_at]->(e2)
return p1, p2, e1, e2
// RESULT:
// (Jim)-works_at->(Microsoft)
// (Jim)-works_at->(Oracle)
// (John)-works_at->(Microsoft)
// (John)-works_at->(Oracle)
爲了COLLECT
你也可以使用文字地圖一個不錯的輸出格式如下面的例子:
MATCH
(a:Employee)-[r:works_at]->(e:Employer)
WITH
a,
r,
COLLECT({name:e.name, id:ID(e)}) AS employers
WITH
a,
COLLECT({ type : type(r), employers : employers}) AS employerRels
WITH
{ name : a.name, id: ID(a), employers : employerRels} AS employee
RETURN
employee
然後,真棒結果將很好地格式化JSON這樣的:
{
"name": "Jim",
"id": 227,
"employers": [
{
"type": "works_at",
"employers": [
{
"name": "Oracle",
"id": 226
}
]
},
{
"type": "works_at",
"employers": [
{
"name": "Microsoft",
"id": 225
}
]
}
]
},
{
"name": "John",
"id": 224,
"employers": [
{
"type": "works_at",
"employers": [
{
"name": "Oracle",
"id": 226
}
]
},
{
"type": "works_at",
"employers": [
{
"name": "Microsoft",
"id": 225
}
]
}
]
}
謝謝,那就是我一直在尋找的! –