Chapter 10. Utility Functions

Cypher brings along some basic functions for math, text, collections and maps.

10.18. Atomic Examples

add. Dataset

CREATE (p:Person {name:'Tom',age: 40})

We can add 10 to the property age

MATCH (n:Person {name:'Tom'})
CALL apoc.atomic.add(n,'age',10,5) YIELD oldValue, newValue
RETURN n
apoc.atomic.add

subtract. From the previous example we can go back to age: 40

MATCH (n:Person {name:'Tom'})
CALL apoc.atomic.subtract(n,'age',10,5) YIELD oldValue, newValue
RETURN n

concat. Dataset

CREATE (p:Person {name:'Will',age: 35})
MATCH (p:Person {name:'Will',age: 35})
CALL apoc.atomic.concat(p,"name",'iam',5) YIELD newValue
RETURN p
apoc.atomic.concat

insert. Dataset

we add a propery children that is an array

CREATE (p:Person {name:'Tom', children: ['Anne','Sam','Paul']})
apoc.atomic.insert

Now we add Mary to propery children at the position 2

MATCH (p:Person {name:'Tom'})
CALL apoc.atomic.insert(p,'children',2,'Mary',5) YIELD newValue
RETURN p
apoc.atomic.insert.result

remove. Dataset

CREATE (p:Person {name:'Tom', cars: ['Class A','X3','Focus']})

Now we remove the element X3 which is at the position 1 from the array cars

MATCH (p:Person {name:'Tom'})
CALL apoc.atomic.remove(p,'cars',1,5) YIELD newValue
RETURN p
apoc.atomic.remove

update. Dataset

CREATE (p:Person {name:'Tom', salary1:1800, salary2:1500})

We want to update salary1 with the result of an expression. The expression always have to be referenced with the n. that refers to the node/rel passed as parameter. If we rename our node/rel (as in the example above) we have anyway to refer to it in the expression as n.

MATCH (p:Person {name:'Tom'})
CALL apoc.atomic.update(p,'salary1','n.salary1*3 + n.salary2',5) YIELD newValue
RETURN p
apoc.atomic.update

10.19. Log

10.19.1. Log Procedures

APOC provide a set of store procedures in order to add log functionality:

  • apoc.log.info: logs info message
  • apoc.log.error: logs error message
  • apoc.log.warn: logs warn message
  • apoc.log.debug: logs debug message

Every log procedure has the following signature:

apoc.log.<name>(message, params)

Available configuration:

property type description

apoc.user.log.type

enum[none, safe, raw] (default value safe)

type of logging:

  • node: disable the procedures
  • safe: replace all . and whitespace (space and tab) with underscore and lowercase all characters
  • raw: left the messages as-is

apoc.user.log.window.ops

int (default value 10)

num of logs permitted in a time-window, exceeded this quota every log message will be skip

apoc.user.log.window.time

long (default value 10000)

the length (in milliseconds) of the time-window (default 10 seconds)

Example:

The following call (with the default configuration):

call apoc.log.info('Hello %s', ['World'])

produces the following output into the log:

hello_world