问题
I want to construct a query in cypher that does the following:
- create a relationship between 2 nodes with a property containing a time duration if no relationship exists already
- if a relationship is already present, update the time duration property to be the shortest duration of the two relationships
The compare and update part is something that I am not able to achieve in cypher. So any assistence on this part is greatly appreciated.
回答1:
You need combine MERGE ON CREATE | ON MATCH and CASE expression. For example:
MATCH (A:City {id: 1})
MATCH (B:City {id: 2})
WITH A, B, toInteger(rand()*100) as newDuration
MERGE (A)-[r:next]->(B)
ON CREATE SET r.duration = newDuration
ON MATCH SET r.duration = CASE
WHEN r.duration > newDuration
THEN newDuration
ELSE r.duration
END
RETURN r.duration
来源:https://stackoverflow.com/questions/42716898/update-relationship-property-if-previous-relationship-exists-in-neo4j