Merge

本页描述如何使用 MERGE 子句配合 Cypher®.Merge 类。

要添加 MERGE 子句,首先使用 Pattern 类创建一个有效的模式,并将其传递给 Merge 构造函数。

const movie = new Cypher.Node();
const actor = new Cypher.Node();
const pattern = new Cypher.Pattern(movie, { labels: ["Movie"] }).related({type: ["ACTED_IN"]}).to(actor);

const mergeQuery = new Cypher.Merge(pattern);
const { cypher, params } = matchQuery.build()

这将生成以下 MERGE 子句

MERGE (this:Movie)-[:ACTED_IN]->(this1)

随后,你可以添加其他子句。例如

const mergeQuery = new Cypher.Merge(pattern)
    .where(Cypher.eq(movie.property("name"), new Cypher.Param("my-movie")))
    .return(movie);
MERGE (this:Movie)-[:ACTED_IN]->(this1)
WHERE this.name = $param1
return this

ON CREATEON MATCH

使用方法 onCreateSetonMatchSetMERGE 语句后添加 ON CREATE SETON MATCH SET

const node = new Cypher.Node();

const countProp = node.property("count");
const query = new Cypher.Merge(
    new Cypher.Pattern(node, {
        labels: ["MyLabel"],
    })
)
    .onCreateSet([countProp, new Cypher.Literal(1)])
    .onMatchSet([countProp, Cypher.plus(countProp, new Cypher.Literal(1))]);
MERGE (this0:MyLabel)
ON MATCH SET
    this0.count = (this0.count + 1)
ON CREATE SET
    this0.count = 1
© . This site is unofficial and not affiliated with Neo4j, Inc.