apoc.trigger.nodesByLabel

函数 Apoc 扩展

签名

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

输入参数

名称 类型 默认

labelEntries

ANY?

null

标签 (label)

STRING?

null

使用示例

本节中的示例基于以下图数据

create constraint for (p:Person)
require p.id is unique;

此函数旨在用于 apoc.trigger.install Cypher 语句内部。

我们可以利用它在添加或移除标签,或添加或移除属性时,有条件地执行 Cypher 语句。例如,我们通过定义以下触发器,为所有 Person 节点添加一个 id 属性,其值为该节点 name 属性的小写值:

CALL apoc.trigger.install(
  'neo4j',
  'lowercase',
  'UNWIND apoc.trigger.nodesByLabel($assignedLabels,"Person") AS n
   SET n.id = toLower(n.name)',
  {}
);

现在我们创建一个 Person 节点

CREATE (f:Person {name:'John Doe'});

现在我们来查找所有的 Person 节点

MATCH (f:Person)
RETURN f.id, f.name;
表 1. 结果
f.id f.name

"john doe"

"John Doe"

但如果我们创建一个 Animal 节点

CREATE (:Animal {name: "Cow"});

该节点将不会被添加 id 属性。我们可以通过运行以下查询来查看它确实未被添加:

MATCH (n)
RETURN n;
表 2. 结果
n

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

(:Animal {name: "Cow"})

© . This site is unofficial and not affiliated with Neo4j, Inc.