Ravi Shanker Reddy
Ravi Shanker Reddy

Reputation: 495

MERGE after checking the condition in neo4j

enter image description here

In this If i deleted "aravind" the node "ramya" has to be connected to "sreepad". For that I written a query

MATCH (m)<-[:createdBy]-(n:Login{UserName:"aravind"})<-[:createdBy]-(z)
merge (m)<-[:createdBy]-(z)  
set z.createdBy=m.UserName 
detach delete n

Its working fine when the node has a tail. But not working on the end user (Suppose "prem"). How to write a query which works for both ???

Thanks in advance

Upvotes: 1

Views: 601

Answers (1)

stdob--
stdob--

Reputation: 29172

You need: 1) Get node for delete 2) Get parent node 3) Collect child nodes 4) Create relationships 5) Delete node

MATCH (n:Login{UserName:"aravind"})
OPTIONAL MATCH (n)->[:createdBy]-(m)
OPTIONAL MATCH (n)<-[:createdBy]-(z)
WITH n, collect(m)[0] as m, collect(z) as zs
 FOREACH(z in zs |
    merge (m)<-[:createdBy]-(z)  
    set z.createdBy=m.UserName
 )
detach delete n

Upvotes: 1

Related Questions