cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Exclude some labels from nodes list of a path

Hello

I am using cypher below to find name of nodes from A to B, and attach the id of path to them.

MATCH p = (n1:A)-[*]->(n2:B)
WHERE not(exists((n2)-[]->(:B)))
UNWIND nodes(p) AS n3
RETURN n1.name, n3.name, reduce(x = '0', y IN nodes(p) | x + '_' + id(y)) AS pid

I get output as table below, in which rows(in yellow) containing null value in n3.name column are not expected to be output.

n1.name n3.name pid
a010 null 0_10_1_2
a010 b001 0_10_1_2
a010 b002 0_10_1_2
a010 null 0_10_1_3
a010 b001 0_10_1_3
a010 b003 0_10_1_3

How can I exclude these rows from output?

1 ACCEPTED SOLUTION

ameyasoft
Graph Maven
Try this:
MATCH p = (n1:A)-[*]->(n2:B)
WHERE not(exists((n2)-[]->(:B)))
UNWIND nodes(p) AS n3
with n1, n2, n3 where n3.name is not null
RETURN n1.name, n3.name, reduce(x = '0', y IN n3 | x + '_' + id(y)) AS pid

View solution in original post

4 REPLIES 4

ameyasoft
Graph Maven
Try this:
MATCH p = (n1:A)-[*]->(n2:B)
WHERE not(exists((n2)-[]->(:B)))
UNWIND nodes(p) AS n3
with n1, n2, n3 where n3.name is not null
RETURN n1.name, n3.name, reduce(x = '0', y IN n3 | x + '_' + id(y)) AS pid

@ameyasoft‘s solution should work. Another approach is to filter the list of nodes that gets unwinded. Replace line 4 with

unwind [n in nodes(p) where n.name is not null] as n3. 

Thanks! This also works, too.

Thank you both!

Thank you! This works!