-1

How can i alter a node with SET if an other node meets a certain condition?

Pseudo-code-example:

MATCH (node1{myId:123456}) // the node which should be compared
RETURN
CASE
WHEN node1.name = "foo" // if condition is fulfilled
THEN MATCH (node2{myId:654321}) SET node2.name = "bar" // the node which i want to edit)
END

I can't use MERGE or simply MATCH (...) SET (...) because 'evaluated node' != 'node which should be manipulated'. CASE expressions don't seem to work either. I've also tried approaches with the FOREACH-CASE hack, obviously without any success.

Note that my use case implies, that both nodes, which are involved, do already exist (and are unique).
Any suggestions?

Community
  • 1
  • 1
nichoio
  • 6,289
  • 4
  • 26
  • 33

1 Answers1

1

For one, you'll want to use labels on your matches, otherwise the matches won't take advantage of the index.

You can solve your comparison problem using just MATCH (or OPTIONAL MATCH) and WHERE (adding on a substitute label here).

MATCH (node1:Node{myId:123456}) 
MATCH (node2:Node{myId:654321}) 
WHERE node1.name = "foo" 
SET node2.name = "bar"

WHERE clauses only apply to the previous MATCH, OPTIONAL MATCH, or WITH clauses.

The above query as it is will have no result rows after the WHERE clause executes if node1's name isn't 'foo', so the SET operation won't have anything to change.

If this is a fragment of a larger query, and you have operations you'd like to perform after regardless of the conditional, use OPTIONAL MATCH instead. It will keep result rows, but node2 will be null if node1's name isn't 'foo', so the SET operation still won't change anything.

InverseFalcon
  • 29,576
  • 4
  • 38
  • 51
  • Ugh, i feel a bit dumb now. Thanks anyway! PS: This was a simplified example, that's why i've omitted labels. – nichoio Mar 26 '17 at 10:12