Create

本页描述如何使用 Cypher®.Create 类添加一个 CREATE 子句。

要添加 CREATE 子句,首先使用 Pattern 类创建一个有效的模式。

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

const matchQuery = new Cypher.Create(pattern);

这将生成以下 CREATE 子句

CREATE (this:Movie)

可以向 Pattern 传递属性,以使用这些属性创建节点。

const movie = new Cypher.Node();
const pattern = new Cypher.Pattern(movie, { labels: ["Movie"], properties: { title: "The Matrix" } });

const matchQuery = new Cypher.Create(pattern);

这将生成以下 CREATE 子句

CREATE (this:Movie {title: "The Matrix"})

关系

通过创建相应的 Pattern,可以在匹配子句中使用关系。

const actedInPattern = new Cypher.Pattern(movieNode, { labels: ["Movie"] })
    .related({ type: "ACTED_IN" })
    .to(personNode, { labels: ["Person"] });

const create = new Cypher.Create(actedInPattern)
CREATE (this1:`Person`)-[this0:ACTED_IN]->(this2:`Movie`)

更新属性

使用方法 .set.remove 分别添加 SETREMOVE 子句。

.set 接受一个或多个元组,每个元组包含要更新的属性及其新值。

const createQuery = new Cypher.Create(new Cypher.Pattern(movie, { labels: ["Movie"] })).set([
    movie.property("title"),
    new Cypher.Param("The Matrix"),
]);
CREATE (this0:Movie)
SET this0.title = $param0

通过传入多个元组可以一次更新多个属性。

.remove 接受要删除的属性。

const createQuery = new Cypher.Create(new Cypher.Pattern(movie, { labels: ["Movie"] })).remove(
    movie.property("title"),
    movie.property("year")
);
CREATE (this0:Movie)
REMOVE this0.title, this0.year
© . This site is unofficial and not affiliated with Neo4j, Inc.