0
votes

I have a neo4j graph with nodes that I need to increment a flag on all of the nodes on paths from a start to end node. But I need to increment this flag only ONCE, even if it lies on multiple paths.

I use the following query, but obviously it loops through all of the paths so the flag is incremented more than once:

MATCH paths = (end:Operation)-[DEPENDS_ON*]->(start:Operation )
       WHERE id(start) = 304
       AND end.final = true 
       UNWIND [ops IN nodes(paths)] AS op
       SET op.flag = op.flag + 1

enter image description here

How can I unwind or collect the distinct nodes on all paths then increment the property?

1

1 Answers

1
votes

What about this?

WITH COLLECT(DISTINCT ops) AS distinctOps
MATCH paths = (end:Operation)-[DEPENDS_ON*]->(start:Operation )
       WHERE id(start) = 304
       AND end.final = true 

UNWIND nodes(paths) AS ops
WITH COLLECT(DISTINCT ops) AS distinctOps
FOREACH (op IN distinctOps |
  SET op.flag = op.flag + 1
)