2016-04-24 35 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))