Sonal Pachpute
Sonal Pachpute

Reputation: 39

fetching a tree for a particular node in neo4j

I want all children of a given node with their interconnection with each other. For example A->B->C->D is a tree and I want a tree from A(starting point), Output should be
A->B
B->C
C->D
and not as
A->B
A->C
A->D

MATCH (fromNode:Part{partNumber:"100"})-[:HAS_BOM_PROPERTY]->(BomNode:BomProperties)-[*]->(toNode:Part) return fromNode{.*}, toNode{.*},BomNode{.*}

I am using above query, it is giving me all the nodes connected with partNumber 100 at all levels but I want them along with their relationship with respective parent element and not all directly connected to partNumber 100.

Upvotes: 1

Views: 264

Answers (1)

Graphileon
Graphileon

Reputation: 5385

What about something like this::

MATCH path=(fromNode:Part{partNumber:"100"})-[:HAS_BOM_PROPERTY]->(BomNode:BomProperties)-[*]->(toNode:Part)
// get all the relationships
UNWIND relationships(path) AS rel
// remove duplicates
WITH DISTINCT rel
RETURN startNode(rel) AS fromNode,endNode(rel) AS toNode

Upvotes: 1

Related Questions