Neo4j Cypher获取循环链表中的所有节点

时间:2013-07-23 03:35:29

标签: neo4j cypher

我有一个看起来像这样的图表

(user)->[:Comments]->(comment)->[:Comments]->(comment)->[:Comments]->(comment)->(user)

基本上它是用户制作的循环链接评论列表。该列表圈出并最终返回给用户。如何使用cypher检索所有注释?

1 个答案:

答案 0 :(得分:2)

对此进行了一次刺杀,看起来对我来说有点复杂,但无论如何它都在这里。

假设从上次评论到用户的关系是:评论

,而不是..->(comment)->(user)

我假设..->(comment)-[:Comments]->(user)

START n=node(1) 
MATCH n-[:Comments*0..]->(c) 
WHERE c<>n 
WITH collect(c) AS allComments,n 
WITH last(allComments) AS lastcomment,n,allComments 
WHERE lastcomment-[:Comments]->n 
RETURN allComments

我必须输入WHERE c&lt;&gt; n,因为上一个评论 - &gt;用户关系是评论。如果它是别的东西,那就更好了,不需要这个(它只是选择链中的最后一条评论)。它还返回一个集合。

http://console.neo4j.org/r/17d1fy

Bet @Wes Freeman有一个更加狡猾的解决方案

相关问题