我正在努力描述Neo4j中的树结构数据集.在我当前的模型中,一个节点可以有n个链接到其他节点,使其成为这些节点的子节点:
root
|
\-- A
|
\-- 1
|
\-- 2
Run Code Online (Sandbox Code Playgroud)
因为我使用的是nodejs(带有node-neo4j),所以读取数据库仅限于使用Cypher.要读取节点,我使用以下查询:
START n=node(1)
-- this is the root node
MATCH (n)<-[linkedby:links*]-x, x-[linksto?:links*1]->()
-- get all nodes that link to the root node and all nodes linking to those nodes, etc, etc and retrieve all nodes those found nodes link to
RETURN n, x, linkedby, LAST(linkedby) AS parent, COLLECT(DISTINCT linksto) AS links
-- the last in the path of linkedby is the …Run Code Online (Sandbox Code Playgroud)