I wanna know how can I make property value of one node change simultaneously when other node’s value changes.
For example, I have three nodes called "Andy", "Michael" and "Family". "Andy" and "Michael" are couple, and they are both part of the "Family". Here are the codes:
MATCH (a:Person),(b:Person)
WHERE a.name = 'Andy' AND b.name = 'Michael'
CREATE (a)-[r:FRIEND_OF]->(b)
RETURN *
MATCH (a:Person),(c:Person)
WHERE a.name = 'Andy' AND c.name = 'Family'
CREATE (a)-[r:PART_OF]->(c)
RETURN *
MATCH (b:Person),(c:Person)
WHERE b.name = 'Michael' AND c.name = 'Family'
CREATE (b)-[r:PART_OF]->(c)
RETURN *
Then, I wanna get the sum of pay from both Andy and Michael, and put the value into the "sum_pay" in "Family". So I use "set" to make "sum_pay = pay of Andy + pay of Michael". After that, I change the pay of Andy, however, the value of sum_pay does not change, which is supposed to change according to human understanding.
So, I wanna ask if there is any solution in Cypher that can help to change the value of sum_pay automatically while the value of pay of Andy and pay of Michael change. Many thanks!
Have a look at APOC triggers - they are intended for situations where you want to make changes to the graph that depend on a change that just arrived.
In your case, the trigger would execute on assignedNodeProperties, and the trigger code can just be regular cypher, so setting the sum on the family should be straightforward.
call apoc.trigger.add("cal_total","MATCH (s:Student)-[r:HAS_SUBJECT]->(sub:Subject)
WITH s, sum(sub.marks) as total
set s.mark = total",{phase:'before'});