2016-04-24 76 views
2

在我的數據庫節點我的ID列表以及檢索節點列表:用戶節點,它們的關係爲:友誼關係。我想要得到這樣的結構:與直接關係到他們的Neo4j

[ 
    { 
     id: 1, 
     username: "Whatever", 
     email: "[email protected]" 
     ... 
    }, 
    [ 6, 7, 8, ... ] 
], 
[ 
    { 
     id: 2, 
     username: "Another user", 
     email: "[email protected]" 
     ... 
    }, 
    [ 15, 16, 17, 18, ... ] 
], 
... 

...其中數字是節點與a:友誼關係直接相關的節點的ID。

這個答案有一定的查詢幾乎做的工作:

Can I find all the relations between two nodes in neo4j?

但最近的一個,我想出了是:

match p=(a:User)-[:Friendship]->(d:User) 
return d, reduce(nodes = [],n in nodes(p) | nodes + [id(n)]) as node_id_col 

...返回這種結構:

[ 
    { 
     id: 1, 
     username: "Whatever", 
     email: "[email protected]" 
     ... 
    }, 
    [ 1, 6 ] 
], 
[ 
    { 
     id: 1, 
     username: "Whatever", 
     email: "[email protected]" 
     ... 
    }, 
    [ 1, 7 ] 
], 
[ 
    { 
     id: 1, 
     username: "Whatever", 
     email: "[email protected]" 
     ... 
    }, 
    [ 1, 8 ] 
], 
[ 
    { 
     id: 2, 
     username: "Another user", 
     email: "[email protected]" 
     ... 
    }, 
    [ 2, 15 ] 
], 
[ 
    { 
     id: 2, 
     username: "Another user", 
     email: "[email protected]" 
     ... 
    }, 
    [ 2, 16 ] 
], 
... 

這並不好,因爲它返回大量冗餘數據。

那麼適當的Cypher查詢會是什麼?

謝謝!

回答

2

我認爲你可能會過於複雜的事情或我沒有正確理解問題。像這樣的東西對你有用嗎?

match (a:User)-[:Friendship]->(d:User) 
return a, collect(id(d))