apoc.cypher.doIt
Syntax |
|
||
Description |
Runs a dynamically constructed statement with the given parameters. This procedure allows for both read and write statements. |
||
Input arguments |
Name |
Type |
Description |
|
|
The Cypher statement to run. |
|
|
|
The parameters for the given Cypher statement. |
|
Return arguments |
Name |
Type |
Description |
|
|
The result returned from the Cypher statement. |
This procedure cannot perform |
Using dynamic labels in Cypher
Node labels and relationship types can be referenced dynamically in Cypher without using APOC.
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;
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:
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;
MATCH (node)
WITH node, labels(node)[0] AS label
REMOVE node:$(label)
SET node:$(apoc.text.capitalize(toLower(label)))
RETURN node AS value;
value |
---|
{node: (:Person)} |
{node: (:Event)} |
{node: (:Tag)} |
{node: (:Location)} |
For schema operations, it is necessary to use the apoc.cypher.runSchema procedure. |