Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
07-13-2020 01:56 AM
Hi,
I am trying to delete only some of the relationships attached to a node while not deleting those that I have matched by using MATCH then using WHERE NOT EXISTS.
In the example below, I have already created a node (:Person {name:'John'}) and then I MERGE 3 nodes (:Car) with names (test1, test2, test3). However, when I run the query it doesn't delete the intended relationship but instead it deletes all relationships.
UNWIND [{name:'test1'}, {name:'test2'}] AS test
MATCH (p:Person {name:'John'})
OPTIONAL MATCH (p)-[d:DRIVES]->(c:Car)
WHERE NOT EXISTS((p)-[:DRIVES]->(c:Car {name:test.name}))
DELETE d
RETURN p
Thanks in advance .
Solved! Go to Solution.
07-13-2020 10:09 AM
I suggest you to do like this otherwise it will deletes all the elements of the list but keeping the first:
MATCH (p:Person {name:'John'})-[d:DRIVES]->(c:Car)
WHERE NOT c.name IN ['test1', 'test2']
DETACH DELETE d
Regards,
Cobra
07-13-2020 02:01 AM
Hello @tarendran.vivekanand
UNWIND [{name:'test1'}, {name:'test2'}] AS test
MATCH (p:Person {name:'John'})-[d:DRIVES]->(c:Car)
WHERE NOT EXISTS((p)-[:DRIVES]->(c:Car {name:test.name}))
DETACH DELETE d
Regards,
Cobra
07-13-2020 02:17 AM
Hello @Cobra
Unfortunately the query you gave still deletes all relationships
Basically im trying to delete all relationships except those that are in the list
Also, I just tested the query works when the list is just 1
UNWIND [{name:'test1'}] AS test
07-13-2020 10:09 AM
I suggest you to do like this otherwise it will deletes all the elements of the list but keeping the first:
MATCH (p:Person {name:'John'})-[d:DRIVES]->(c:Car)
WHERE NOT c.name IN ['test1', 'test2']
DETACH DELETE d
Regards,
Cobra
07-13-2020 07:23 PM
Thanks @Cobra
However there was a slight error NOT has to come first otherwise will get error.
MATCH (p:Person {name:'John'})-[d:DRIVES]->(c:Car)
WHERE NOT c.name IN ['test1', 'test2']
DETACH DELETE d
07-14-2020 12:17 AM
Oh yeah, Python habits
I updated my answer, thank you
All the sessions of the conference are now available online