构建类型谓词表达式

本节概述了在查询中使用 Cypher® Builder 创建类型谓词表达式的过程。

类型谓词表达式用于验证变量、字面量、属性或其他 Cypher 表达式的类型。它遵循以下 Cypher 语法

<expr> IS :: <TYPE>

例如:

movie.title IS :: STRING

isType

类型谓词表达式可以使用 Cypher.isType 构造(与关系的 .hasType 方法不同)。isType 函数接受一个 Cypher 变量 和在 Cypher.TYPE 中指定的类型

Cypher.isType(new Cypher.Variable(), Cypher.TYPE.INTEGER);
var0 IS :: INTEGER

类型谓词表达式可以在 WHERE 语句中使用,例如

const node = new Cypher.Node();
const matchClause = new Cypher.Match(node).where(Cypher.isType(node.property("title"), Cypher.TYPE.STRING)).return(node);
MATCH (this0)
WHERE this0.title IS :: STRING
RETURN this0

使用联合类型

例如 INTEGER | STRING 的联合类型,可以通过向 .isType 传递数组来使用

Cypher.isType(new Cypher.Variable(), [Cypher.TYPE.INTEGER, Cypher.TYPE.STRING]);
var0 IS :: INTEGER | STRING

列表类型

例如 LIST<INTEGER> 的列表类型,可以使用 Cypher.TYPES.list() 创建

Cypher.isType(new Cypher.Variable(), Cypher.TYPE.list(Cypher.TYPE.STRING));
var0 IS :: LIST<STRING>

联合类型也可以在列表中使用

Cypher.isType(new Cypher.Variable(), Cypher.TYPE.list([Cypher.TYPE.STRING, Cypher.TYPE.INTEGER]));
var0 IS :: LIST<STRING | INTEGER>

带 NOT 的类型谓词表达式

也可以使用函数 Cypher.isNotType 验证 Cypher 表达式不属于某种类型

Cypher.isNotType(new Cypher.Variable(), Cypher.TYPE.INTEGER);
var0 IS NOT :: INTEGER

用于非空类型的类型谓词表达式

在 Cypher 中,类型谓词表达式对 NULL 值的计算结果为 true,除非追加 NOT NULL。在 Cypher Builder 中,可以使用 .notNull 方法实现

Cypher.isType(new Cypher.Variable(), Cypher.TYPE.INTEGER).notNull();
var0 IS :: INTEGER NOT NULL

非空类型也可以在列表类型中使用

Cypher.isType(new Cypher.Variable(), Cypher.TYPE.list(Cypher.TYPE.STRING).notNull());
var0 IS :: LIST<STRING NOT NULL>
© . This site is unofficial and not affiliated with Neo4j, Inc.