apoc.trigger.nodesByLabel

Function Apoc Extended

Signature

apoc.trigger.nodesByLabel(labelEntries :: ANY?, label :: STRING?) :: (LIST? OF ANY?)

Input parameters

Name Type Default

labelEntries

ANY?

null

label

STRING?

null

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;
Table 1. Results
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;
Table 2. Results
n

(:Person {name: "John Doe", id: "john doe"})

(:Animal {name: "Cow"})