apoc.trigger.nodesByLabel
This is the APOC Extended documentation. APOC Extended is not supported by Neo4j. For the officially supported APOC Core, go to the APOC Core page. |
Function Apoc Extended
Usage Examples
The examples in this section are based on the following graph:
create constraint for (p:Person)
require p.id is unique;
This function is used inside an apoc.trigger.add Cypher statement.
We can use it to conditionally run Cypher statements when labels are added or removed or when properties are added or removed.
For example, we add an id
property to all Person
nodes that is the lower case value of the name
property of that node, by defining the following trigger:
CALL apoc.trigger.add(
'lowercase',
'UNWIND apoc.trigger.nodesByLabel($assignedLabels,"Person") AS n
SET n.id = toLower(n.name)',
{}
);
Let’s now create a Person
node:
CREATE (f:Person {name:'John Doe'});
And now let’s find all Person
nodes:
MATCH (f:Person)
RETURN f.id, f.name;
f.id | f.name |
---|---|
"john doe" |
"John Doe" |
But if we create an Animal
node instead:
CREATE (:Animal {name: "Cow"});
An id
property will not be added to that node.
We can see that it hasn’t been added, by running the following query:
MATCH (n)
RETURN n;
n |
---|
(:Person {name: "John Doe", id: "john doe"}) |
(:Animal {name: "Cow"}) |