cypher return twice for nodes that connected in bi-directional relationship

牧云@^-^@ 提交于 2019-12-10 17:25:13

问题


I have 2 nodes: (A), (B), connected by [:FRIEND]

When I run the following command,

start n = node(*) match (n)-[r:FRIEND]-(b) return n.name, b.name;

it returns 2 rows: A, B and B, A.

I wonder, how to make it return only one record, because the relationship is bidirectional, A -[:FRIEND]-B and B-[:FRIEND]-A is considered same result.

Thanks.


回答1:


One trick is to add a where on the IDs, so you get them in a consistent order as well:

start n = node(*) 
match (n)-[r:FRIEND]-(b) 
where id(n) < id(b) 
return n.name, b.name;

http://console.neo4j.org/r/1ry0ga

If you have multiple relationships between them (in both directions, for example), you can add a distinct modifier to get the same results:

start n = node(*) 
match (n)-[r:FRIEND]-(b) 
where id(n) < id(b) 
return distinct n.name, b.name;


来源:https://stackoverflow.com/questions/14410127/cypher-return-twice-for-nodes-that-connected-in-bi-directional-relationship

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!