Merge when either property1 is the same OR when property2 is the same

I am trying to basically write a cypher that will use merge with an or condition:

pseudo imaginary code:

if node exists with property id != 0 and (id == 1 or path == "c:\hello")
then set id=1, path="d:\bye", otherProperties="x"
else create node with id=1, path=path="d:\bye", otherProperties="x"

The or is what is making problems for me when using merge (n:Node {id:x, path:y}). Since a merge will always create a new node when one property is different. In my case it should merge the existing node when either id or path are equal.

I came up with the following cypher (which has errors):

optional match (n:Node)
where <> '0'
    ( = '1'
    n.path = "c:\\hello")
with n
CALL = null, 'create (n2:Node {id:1, path:"c:\\bye", otherProperties="x"})','set, path="c:\bye", otherProperties=x',{n:n}) yield value 
with value as ignored
return ignored

What is the correct way to do it?


Not sure if I've understood correctly, but we do recommend if you're going to use MERGE, you will get more predictable results if you MERGE only on the unique property (which I assume is id in this case) and then use either ON CREATE SET or ON MATCH SET to then create/update any additional properties.

It's like an edge case. Because the node basically has two unqiue properties: id or(exclusive) path.
So there can't be nodes that have a duplicate path or id. That's why I can't use 'merge'.

If there is a node that already has the id, please match it.
If there is a node that already has the path, please match it.
If both are unique create a new node.

Maybe something like this before you go into

WITH [n1,n2] as arr
unwind arr as nodes
WITH nodes
CALL = 0 //create a new node...