apoc.cypher.doIt

Details

Syntax

apoc.cypher.doIt(statement, params) :: (value)

Description

Runs a dynamically constructed statement with the given parameters. This procedure allows for both read and write statements.

Input arguments

Name

Type

Description

statement

STRING

The Cypher statement to run.

params

MAP

The parameters for the given Cypher statement.

Return arguments

Name

Type

Description

value

MAP

The result returned from the Cypher statement.

This procedure cannot perform SCHEMA operations. To run a query in SCHEMA mode, use apoc.cypher.runSchema.

Using dynamic labels in Cypher

Node labels and relationship types can be referenced dynamically in Cypher without using APOC.

Cypher syntax for creating, matching and merging labels and types dynamically
CREATE (n1:$(label))-[r:$(type)]->(n2:$(label))
MERGE (n1:$(label))-[r:$(type)]->(n2:$(label))
MATCH (n1:$(label))-[r:$(type)]->(n2:$(label))

The dynamically calculated type must evaluate to a STRING or LIST<STRING>. For more information, see the Cypher Manual → CREATE, MERGE, MATCH.

Usage Examples

The examples in this section are based on a graph where we’ve accidentally created node labels in all capitals, as shown below:

CREATE (:PERSON)
CREATE (:EVENT)
CREATE (:TAG)
CREATE (:LOCATION);

We want to update all these labels to have only the first letter capitalized. We can use the toLower and apoc.text.capitalize functions to transform the label names, as shown in the following query:

CALL db.labels()
YIELD label
RETURN apoc.text.capitalize(toLower(label)) AS value;
Results
value

"Event"

"Person"

"Tag"

"Location"

Now we want to set our new labels and remove the old ones. The procedure apoc.cypher.doIt is one way of dynamically setting and removing labels, but it is also possible to do so using pure Cypher. The following shows equivalent queries:

apoc.cypher.doIt
MATCH (node)
WITH node, labels(node)[0] AS label
CALL apoc.cypher.doIt(
  "WITH $node AS node
   REMOVE node:" + label + "\n" +
  "SET node:" + apoc.text.capitalize(toLower(label)) + "\n" +
  "RETURN node",
  {node: node})
YIELD value
RETURN value;
SET and REMOVE clauses
MATCH (node)
WITH node, labels(node)[0] AS label
REMOVE node:$(label)
SET node:$(apoc.text.capitalize(toLower(label)))
RETURN node AS value;
Results
value

{node: (:Person)}

{node: (:Event)}

{node: (:Tag)}

{node: (:Location)}

For schema operations, it is necessary to use the apoc.cypher.runSchema procedure.