按标签过滤

大多数模式使用标签组合,例如 (m:Movie),以便轻松按标签过滤。您还可以使用 标签表达式 来执行复杂的标签过滤。

.hasLabel

可以在 WHERE 筛选中检查标签是否存在。对 Node 变量使用 .hasLabel.hasLabels 方法。例如,要匹配具有 MovieFilm 标签的节点

const movieNode = new Cypher.Node();

const matchQuery = new Cypher.Match(new Cypher.Pattern(movieNode))
    .where(Cypher.or(movieNode.hasLabel("Movie"), movieNode.hasLabel("Film")))
    .return(movieNode);
MATCH (this0)
WHERE (this0:Movie OR this0:Film)
RETURN this0

方法 .hasLabels 允许您提供节点必须具备的多个标签

const movieNode = new Cypher.Node();

const matchQuery = new Cypher.Match(new Cypher.Pattern(movieNode))
    .where(movieNode.hasLabels("Movie", "Film"))
    .return(movieNode);
MATCH (this0)
WHERE this0:Movie:Film
RETURN this0

.hasType

与标签类似,关系类型也可以在 WHERE 语句中使用 .hasType 方法进行过滤

const personNode = new Cypher.Node();
const actedIn = new Cypher.Relationship();

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

const matchQuery = new Cypher.Match(actedInPattern)
    .where(Cypher.or(actedIn.hasType("ACTED_IN"), actedIn.hasType("DIRECTED")))
    .return(personNode);
MATCH (:Movie)-[this0]->(this1:Person)
WHERE (this0:ACTED_IN OR this0:DIRECTED)
RETURN this
© . This site is unofficial and not affiliated with Neo4j, Inc.