Atomic Property Updates

Atomic procedures handle the concurrency, it’s add a lock to the resource. If two users access to the same resource at the same time, with the parameter times (default value 5) we can determine how many time retry to modify the resource, until the lock is release.

Qualified Name Type Release

apoc.atomic.add

`apoc.atomic.add(node/relatonship,propertyName,number) Sums the property’s value with the 'number' value `

Procedure

APOC Core

apoc.atomic.subtract

apoc.atomic.subtract(node/relatonship,propertyName,number) Subtracts the 'number' value to the property’s value

Procedure

APOC Core

apoc.atomic.concat

apoc.atomic.concat(node/relatonship,propertyName,string) Concats the property’s value with the 'string' value

Procedure

APOC Core

apoc.atomic.insert

apoc.atomic.insert(node/relatonship,propertyName,position,value) insert a value into the property’s array value at 'position'

Procedure

APOC Core

apoc.atomic.remove

apoc.atomic.remove(node/relatonship,propertyName,position) remove the element at position 'position'

Procedure

APOC Core

apoc.atomic.update

apoc.atomic.update(node/relatonship,propertyName,updateOperation) update a property’s value with a cypher operation (ex. "n.prop1+n.prop2")

Procedure

APOC Core

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

The expression always have to be referenced with the n. that refers to the entity passed as the first parameter. If we rename our node/rel (as in the example above) we have anyway to refer to it in the expression as n.

Table 6. Results
p

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