Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

set new relationship and save previous relationship as old relationship in neo4j

I’m trying to transfer data from sql database to neo4j.
I have created company nodes, number nodes and relationship between them in neo4j.

(n:company{un_id:’11111’})-[r:using]->(m:phonenumber{N:‘555112233’})

Sometimes, companies are changing its number and I want to set new relationship like

Match
(n:company{un_id:’11111’}),
(m:phonenumber{N:‘555445566’})
Merge
(n)-[r:using]->(m)
Return id(r)

So if there is no more relationship between company and old phone number, how can I set previous relationship as old rel?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Using a status property on the relationship could help you, for instance like this

WITH '555445566' AS newNumber,
       '11111' AS companyID

//set existing r.status to 'old'
MATCH (n:company {un_id:companyID})-[r:using]->(ph:phonenumber)
WHERE ph.N <> newNumber AND r.status <> 'old'
SET r.status = 'old'

// make sure to remove duplicates in case previous statement returns multiples
WITH DISTINCT newNumber, n

// create the new rel
MERGE (newph:phonenumber {N:newNumber})
MERGE (n)-[r:using {status:'current'}]->(newph) 
RETURN id(r)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading