Atomic property updates

The APOC library contains procedures that can be used for thread-safe updating of properties on nodes and relationships.

Procedures for thread-safe updating of properties on nodes and relationships

Qualified Name Type

apoc.atomic.add(container ANY, propertyName STRING, value INTEGER | FLOAT, retryAttempts INTEGER) - sets the given property to the sum of itself and the given INTEGER or FLOAT value. The procedure then sets the property to the returned sum.

Procedure

apoc.atomic.concat(container ANY, propertyName STRING, string STRING, retryAttempts INTEGER) - sets the given property to the concatenation of itself and the STRING value. The procedure then sets the property to the returned STRING.

Procedure

apoc.atomic.insert(container ANY, propertyName STRING, position INTEGER, value ANY, retryAttempts INTEGER) - inserts a value at position into the LIST<ANY> value of a property. The procedure then sets the result back on the property.

Procedure

apoc.atomic.remove(container ANY, propertyName STRING, position INTEGER, retryAttempts INTEGER) - removes the element at position from the LIST<ANY> value of a property. The procedure then sets the property to the resulting LIST<ANY> value.

Procedure

apoc.atomic.subtract(container ANY, propertyName STRING, number INTEGER | FLOAT, retryAttempts INTEGER) - sets the property of a value to itself minus the given INTEGER or FLOAT value. The procedure then sets the property to the returned sum.

Procedure

apoc.atomic.update(container ANY, propertyName STRING, operation STRING, retryAttempts INTEGER) - updates the value of a property with a Cypher operation.

Procedure

Examples

The below examples will further explain these procedures.

The following creates sample nodes:
CREATE (p:Person {name:'Tom',age: 40})
CREATE (p:Person {name:'Will',age: 35})
CREATE (p:Person {name:'David', children: ['Anne','Sam','Paul']})
CREATE (p:Person {name:'John', cars: ['Class A','X3','Focus']})
CREATE (p:Person {name:'Ryan', salary1:1800, salary2:1500})
The following adds 10 to the property age for Tom:
MATCH (n:Person {name:'Tom'})
CALL apoc.atomic.add(n,'age',10,5)
YIELD oldValue, newValue
RETURN n
Table 1. Results
n

{"name":"Tom","age":50}

The following subtracts 10 from the property age for Tom:
MATCH (n:Person {name:'Tom'})
CALL apoc.atomic.subtract(n,'age',10,5)
YIELD oldValue, newValue
RETURN n
Table 2. Results
n

{"name":"Tom","age":40}

The following concatenates iam to the name property for Will:
MATCH (p:Person {name:'Will',age: 35})
CALL apoc.atomic.concat(p,"name",'iam',5)
YIELD newValue
RETURN p
Table 3. Results
p

{"name":"William","age":35}

The following adds Mary in position 2 of children list:
MATCH (p:Person {name:'David'})
CALL apoc.atomic.insert(p,'children',2,'Mary',5)
YIELD newValue
RETURN p
Table 4. Results
p

{"name":"David","children":["Anne","Sam","Mary","Paul"]}

The following removes the element X3, which is at position 1, from the array cars
MATCH (p:Person {name:'John'})
CALL apoc.atomic.remove(p,'cars',1,5)
YIELD newValue
RETURN p
Table 5. Results
p

{"name":"John","cars":["Class A","Focus"]}

The following updates salary1 with the result of an expression:
MATCH (p:Person {name:'Ryan'})
CALL apoc.atomic.update(p,'salary1','n.salary1*3 + n.salary2',5)
YIELD newValue
RETURN p

In the operation expression (3rd parameter) the entity passed in as container (1st parameter) is referred to using the variable n. If the node or relationship is renamed (as in the example above), it is still necessary to refer to it in the expression as n.

Table 6. Results
p

{"name":"Ryan","salary1":6900,"salary2":1500}