如果你想查询特定节点类型和相关节点解决方案很简单,通过使用collect函数我们可以实现这个目标,如return country,collect(city)as c
但是,如果我们需要检索像血统或用户的数据树,那么我们应该怎么做 - > post-> comment-> like 有什么解决方案可以在cypher输出中处理这种数据吗?
答案 0 :(得分:3)
给出以下图表:
CREATE (user:User { id: 0 })
CREATE (post:Post)
CREATE (comment:Comment)
CREATE (user)-[:POSTED]->(post)<-[:ON]-(comment)<-[:COMMENTED]-(user)
CREATE (user)-[:LIKES]->(comment)
使用以下查询检索可变长度路径:
MATCH (user:User { id: 0 })
MATCH p=(user)-[*]->(post)
RETURN p
ORDER BY length(p) DESC
这导致以下输出:
+----------------------------------------------------------------+
| p |
+----------------------------------------------------------------+
| [Node[6]{id:0},:COMMENTED[8] {},Node[8]{},:ON[7] {},Node[7]{}] |
| [Node[6]{id:0},:LIKES[9] {},Node[8]{},:ON[7] {},Node[7]{}] |
| [Node[6]{id:0},:POSTED[6] {},Node[7]{}] |
| [Node[6]{id:0},:COMMENTED[8] {},Node[8]{}] |
| [Node[6]{id:0},:LIKES[9] {},Node[8]{}] |
+----------------------------------------------------------------+
5 rows
19 ms
要查看相关内容和方式,请运行以下查询:
// What is related, and how
MATCH (a)-[r]->(b)
WHERE labels(a) <> [] AND labels(b) <> []
RETURN DISTINCT head(labels(a)) AS This, type(r) as To, head(labels(b)) AS That
LIMIT 10
结果如下:
+-------------------------------------+
| This | To | That |
+-------------------------------------+
| "User" | "POSTED" | "Post" |
| "User" | "COMMENTED" | "Comment" |
| "User" | "LIKES" | "Comment" |
| "Comment" | "ON" | "Post" |
+-------------------------------------+
4 rows
139 ms