Hello I have a graph db like this one:
my wish is to retrieve all the nodes connected through the r
relationship starting from a selected node. Eg
if I select a
, then I expect two rows as a response:
a,b,c
a,d
if I select b
then I expect one row:
b,c
I tried with match p=(:a)-[:r*..]->() return p
but it returns this
and in python I get this that i don't know how to parse:
<Path start=<Node id=93 labels=frozenset({'a'}) properties={'name': 'A'}> end=<Node id=94 labels=frozenset({'b'}) properties={'name': 'B'}> size=1>
<Path start=<Node id=93 labels=frozenset({'a'}) properties={'name': 'A'}> end=<Node id=95 labels=frozenset({'c'}) properties={'name': 'C'}> size=2>
<Path start=<Node id=93 labels=frozenset({'a'}) properties={'name': 'A'}> end=<Node id=96 labels=frozenset({'d'}) properties={'name': 'D'}> size=1>
Edit: I discovered function nodes() so I tried with match p=(:a)-[:r*..]->() return nodes(p)
that returns kinda what I need:
but it still duplicates the path a,b,c
in a,b
and a,b,c
. How can I remove the first row?
Regarding the question of duplicate path, one trick is to exclude paths whose end node is not a leaf, ie there are relationships starting from it. Eg:
MATCH p=(parent {name: "a"})-[:r*1..10]->(child)
WHERE NOT (child)-[:r]->()
RETURN p
For your second question about the return format, you can use a combination of UNWIND
and collect
like this:
MATCH p=(parent {name: "a"})-[:r*1..10]->(child)
WHERE NOT (child)-[:r]->()
UNWIND nodes(p) as node
WITH p, collect(node.name) as names
RETURN names
The result is:
╒═════════════╕
│"names" │
╞═════════════╡
│["a","b","c"]│
├─────────────┤
│["a","d"] │
└─────────────┘
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.