映射

Cypher® 映射 可以使用 Cypher Builder,通过使用 Cypher.Map 类,并传入一个包含字段及相应 Cypher 表达式的对象来构建。

const map = new Cypher.Map({
    foo: new Cypher.Literal("barr"),
    var: new Cypher.Variable(),
    param: new Cypher.Param("test"),
});

此映射可在任何接受表达式的场景中使用,以转换为 Cypher 中的字面映射。

{ foo: "barr", var: var0, param: $param0 }

Cypher.Map 类提供以下方法

  • size:返回映射的当前大小。

  • set:向映射中添加更多字段。此方法支持两种参数形式:两个参数(key,value)或一个包含要添加字段的对象。

映射投影

映射投影 可以在 Cypher Builder 中以类似映射的方式创建,使用 Cypher.MapProjection 类并提供目标变量和投影字段。

const mapProjection = new Cypher.MapProjection(new Cypher.Variable(), ["name", "title"]);

此映射可在任何接受表达式的场景中使用,以转换为 Cypher 中的字面映射。

var0 { .name, .title }

使用 .* 的映射投影

语法 {.*} 允许创建一个包含投影目标中所有元素的映射投影。可以通过在投影字段中传入 \* 来实现。

const mapProjection = new Cypher.MapProjection(new Cypher.Variable(), "*");

此映射可在任何接受表达式的场景中使用,以转换为 Cypher 中的字面映射。

var0 { .* }

在投影字段数组中使用 "*" 字符串会产生不同的效果,因为它会被解释为名为 * 的字段,并相应地进行转义。

const mapProjection = new Cypher.MapProjection(new Cypher.Variable(), ["*"]);
var0 { .`*` }

为映射投影设置额外字段

映射投影还可以包含目标变量中不存在的额外字段,这些字段在投影中被设置。可以通过向 Cypher.MapProjection 传入第三个参数来实现。

const mapProjection = new Cypher.MapProjection(new Cypher.Variable(), ["name", "title"], {
    anotherField: new Cypher.Literal("Another Field")
});
var0 { .name, .title, anotherField: "Another Field" }
© . This site is unofficial and not affiliated with Neo4j, Inc.