列表

可以使用 Cypher.List 并传入 Cypher 表达式数组来创建 Cypher® 列表。例如

new Cypher.Return(new Cypher.List([new Cypher.Literal(1), new Cypher.Literal(2)]));
RETURN [1, 2]

也可以在列表中包含更复杂的表达式

new Cypher.List([Cypher.labels(node)]));
RETURN [labels(node)]

Cypher.Literal 是创建简单字面量列表的更简洁方式,胜过 Cypher.List。你可以改用 new Cypher.Literal(["element 1", "element 2"]) 来避免冗长的代码。

索引

使用变量或 List.index 方法来创建索引访问

myVariable.index(2)
var0[2]

任意表达式的索引运算符

要在任意表达式(如函数)上创建索引,请使用 Cypher.listIndex

Cypher.listIndex(Cypher.collect(new Cypher.Variable()), 2);
collect(var0)[2]

范围

使用 List.range 方法来创建列表范围运算符

new Cypher.List([1,2,3]).range(2, -1)
[1, 2, 3][2..-1]

任意表达式的范围运算符

要在任意表达式(如函数)上创建范围,请使用 Cypher.listRange

Cypher.listRange(Cypher.collect(new Cypher.Variable()), 1, -1);
collect(var0)[1..-1]

列表推导式

列表推导式 可以使用 new Cypher.ListComprehension 并传入用于推导的变量来创建。你还需要一个产生原始列表的表达式,以便从中创建新列表。

const listComprehension = new Cypher.ListComprehension(variable).in(new Cypher.Literal([1,2]))
[var0 IN [1,2]]

通过使用 wheremap 方法,你可以构建推导式的过滤和映射部分

const listComprehension = new Cypher.ListComprehension(variable)
    .in(exprVariable)
    .where(andExpr)
    .map(Cypher.plus(variable, new Cypher.Literal(1)));
[var0 IN $param1 WHERE var0 = $param0 | (var0 + 1)]

模式推导式

模式推导式 可以使用 new Cypher.PatternComprehension 创建,传入一个 模式 和(如果需要)一个映射表达式。

const movie = new Cypher.Node();
const rel = new Cypher.Relationship();
const actor=new Cypher.Node()

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


const comprehension = new Cypher.PatternComprehension(pattern, actor.property("name"));
[(this0:Movie)-[:ACTED_IN]->(this1:Actor) | this1.name]

可以使用 .where 方法添加过滤器

const movie = new Cypher.Node();
const rel = new Cypher.Relationship();
const actor = new Cypher.Node();

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

const comprehension = new Cypher.PatternComprehension(pattern, actor.property("name")).where(
    Cypher.contains(movie.property("title"), new Cypher.Literal("Matrix"))
);
[(this0:Movie)-[this2:ACTED_IN]->(this1:Actor) WHERE this0.title CONTAINS "Matrix" | this1.name]"
© . This site is unofficial and not affiliated with Neo4j, Inc.