I have a node which has property span_id
, contact
and count
, where span_id
is a fixed string, but contacts
is an array of contact_id, and count
is an integer.
My question is if Cypher query can update the node by appending new elements in the array, and aggregated the count
?
For example, I created a node as follows
(s:Service {span_id:"A", contacts:["A1"], count=1}
Later, I got an event which has contact_id = "B2", and span_id
is still "A". Now the node needs to be updated as
(s:Service {span_id:"A", contacts:["A1", "B2"], count=2}
Later again, another event has contact_id = "C3", span_id
= "A", so the node needs to be updated
(s:Service {span_id:"A", contacts:["A1", "B2", "B3"], count=3}
....
Does Cypher support the updating?
You can add an item to a list using the addition operator. And you can set a property based on the length of the list.
WITH ["C3"] as mynewelement
MATCH (n {span_id:'A'})
SET n.count = size(n.contacts+mynewelement)
SET n.contact = = n.contacts + mynewelement