Question

With cypher, I want to create a relationship between two nodes (nodeA and nodeC).

First I have to check if the relationship (nodeA :OWNS nodeC) exists or not. If there is no relationship I want to remove :OWNS relationship nodeA might have with other another node (eg. nodeA :OWNS nodeB) and create :OWNS between nodeA and nodeC.

Is there an efficient way for me to do this with Cypher?

Thank you.

edit: I've accepted an answer but a quicke.. If nodeA had more than one [:OWNS] relationship, by providing specific id for nodeB, it'll only remove the [:OWNS] from nodeB and swap it with nodeC, right?

Was it helpful?

Solution

I don't think Cypher lets you do anything fancy besides SET for MERGE's "ON CREATE" and "ON MATCH".

One approach might be to do this in separate Cypher queries.

However, this might also do the trick:

MATCH (nodeA {id: <nodeA_id>})-[r:OWNS]->(nodeB), (nodeC {id: <nodeC_id>})
WHERE (nodeB) <> (nodeC)
MERGE (nodeA)-[:OWNS]->(nodeC)
DELETE r

And, if at all possible, do try to use labels for identifying your nodes, e.g. NodeA:User. This will speed things up greatly.

Let us know if this works for you.

HTH

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top