Set

此页面描述了如何创建 SET 子句。使用方法 .setSET 语句追加到子句中。

使用 SET 更新属性

您可以使用 .set 方法更新节点或关系的 属性。例如,要更新节点的属性

const personNode = new Cypher.Node();

const matchQuery = new Cypher.Match(new Cypher.Pattern(personNode, { labels: ["Person"] })).set(
    [personNode.property("name"), new Cypher.Param("Keanu")],
);

这将生成如下 Cypher®

MATCH (this0:Person)
SET
    this0.name = $param1
RETURN this0

您可以传入多个属性表达式对,以一次性更新多个属性

const personNode = new Cypher.Node();

const matchQuery = new Cypher.Match(new Cypher.Pattern(personNode, { labels: ["Person"] })).set(
    [personNode.property("name"), new Cypher.Param("Keanu")],
    [personNode.property("year"), new Cypher.Param(1999)]
);

这将生成如下 Cypher

MATCH (this0:Person)
SET
    this0.name = $param1,
    this0.year = $param2
RETURN this0

使用 SET 替换所有节点属性

要更新节点或关系的所有属性,而不是单个属性,请将变量直接作为数组的第一个元素传入。第二个元素必须是 Cypher.Map 或另一个变量

const personNode = new Cypher.Node();

const matchQuery = new Cypher.Match(new Cypher.Pattern(personNode, { labels: ["Person"] })).set([
    personNode, new Cypher.Map({
        name: new Cypher.Param("Keanu"),
        year: new Cypher.Param(1999)
    })
]);

这将生成如下 Cypher

CREATE (this0:Movie)
SET
    this0 = { title: $param0, year: $param1 }

在使用节点时

const matchQuery = new Cypher.Match(new Cypher.Pattern(personNode, { labels: ["Person"] })).set([
    personNode, anotherPersonNode
]);
CREATE (this1:Movie)
SET
    this1 = this0

+= 运算符

在使用 SET 将映射传递给节点以更新属性时,可以使用 += 运算符来添加新属性,而不是替换已有属性

const personNode = new Cypher.Node();

const matchQuery = new Cypher.Match(new Cypher.Pattern(personNode, { labels: ["Person"] })).set([
    personNode, "+=", new Cypher.Map({
        name: new Cypher.Param("Keanu"),
        year: new Cypher.Param(1999)
    })
]);

这将生成如下 Cypher

CREATE (this0:Movie)
SET
    this0 += { title: $param0, year: $param1 }

SET 与标签

您可以使用 SET 来更新节点的标签或关系的类型。更多信息,请参阅 如何更新标签

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