Neo4j:在树状图中显示所有连接的节点及其父节点 [英] Neo4j: Display all connected nodes and their parent in tree-like graph

查看:1346
本文介绍了Neo4j:在树状图中显示所有连接的节点及其父节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的neo4j数据库中获得以下布局:

got following layout in my neo4j db:

    a         
  / | \
 b  c  d     
/ \    |
e  f   g     
   |  / \
   h  i  j  

我想做的就是查询所有子节点,例如如果起始节点为 b ,则子节点为 e,f,h .所有节点具有相同的标签(名称)和相同的关系类型.

What I'd like to do is to query for all child nodes e.g. if the start node is b, then the children are e,f,h. All nodes have the same label (name) and the same relationship type.

我可以通过以下方式完成此任务:

I can accomplish this by:

MATCH (n:node {name:'b'}), (a)-[:DEPENDS_ON*]->(n) RETURN DISTINCT a.name as name

问题是,我希望每个孩子都有一个父母".例如:

The problem is, that I'd like to have a "parent" for each child. e.g:

我想知道 h 在输出中是因为它有父级 f ,在其中有 f 是因为父级是 b ,依此类推.

I want to know that h is in the output because it has parent f, f is there because the parent is b and so on.

非常感谢!

彼得

推荐答案

这是一个返回所有不同的父/子对的查询.

Here is a query that returns all the distinct parent/child pairs.

MATCH path=(n:node {name:'b'})<-[:DEPENDS_ON*]-()
WITH NODES(path) AS np
WITH REDUCE(s=[], i IN RANGE(0, LENGTH(np)-2, 1) | s + {p:np[i], c:np[i+1]}) AS cpairs
UNWIND cpairs AS pairs
WITH DISTINCT pairs AS ps
RETURN ps.p, "parent of", ps.c;

这篇关于Neo4j:在树状图中显示所有连接的节点及其父节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆