apoc.cypher.doIt
Procedure APOC Core
apoc.cypher.doIt(fragment, params) yield value - executes writing fragment with the given parameters
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.
Unfortunately the SET
and REMOVE
clauses don’t support dynamically created values, but we can use the apoc.cypher.doIt
command instead, as shown in the query below:
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;
value |
---|
{node: (:Person)} |
{node: (:Event)} |
{node: (:Tag)} |
{node: (:Location)} |
Please note that if you want to use schema operation, you have to use apoc.cypher.runSchema procedure