I am writing the below Neo4j Cypher query to add new Part
and remove old Part
from Vehicle
node. I am using Neo4j (version 3.4.1)
newParts
and removedParts
are my parameters. They are lists of String
.
with {newParts} AS newParts
unwind newParts as newPart
match (r:Vehicle) where id(r)=639
merge (r)-[:HAS_PART]->(np:Part{name:newPart})
on create np+={lastModifiedDateTime:localdatetime(), createdDateTime:localdatetime()}
with r optional match (r)-[rel:HAS_PART]-(p:Part) where p.name in {removedParts}
delete rel
with r match q=(r)--()
return nodes(q), relationships(q))
It works fine when I provide newParts parameter as non-empty.
However when this is empty, I don't get back my final nodes and relationships. I do understand why that is happening because unwind stops execution when the list is empty. I tried moving with..unwind
part below del
and it successfully deletes removedParts Part
.
However it doesn't return final nodes and relationship as they come after unwind.
I am not sure how to make this work with empty newParts parameter as well. I was trying to use case
but I think case
doesn't work with nodes and relationships.
Any help or pointers would be highly appreciated.
Thanks in advance
V
case size(newParts) ...
? – rickhg12hs