|| apoc.convert.toList - APOC 核心文档 - Neo4j 文档

apoc.convert.toList

详情

语法

apoc.convert.toList(value)

描述

将给定值转换为 LIST<ANY> 类型。

参数

名称

类型

描述

value

ANY

要转换为列表的值。

返回值

LIST<ANY>

使用示例

本节中的示例基于以下示例图

CREATE (Keanu:Person {name:'Keanu Reeves', born:1964})
CREATE (TomH:Person {name:'Tom Hanks', born:1956})

CREATE (TheMatrix:Movie {title:'The Matrix', released:1999, tagline:'Welcome to the Real World'})
CREATE (TheMatrixReloaded:Movie {title:'The Matrix Reloaded', released:2003, tagline:'Free your mind'})

CREATE (YouveGotMail:Movie {title:"You've Got Mail", released:1998, tagline:'At odds in life... in love on-line.'})

CREATE (Keanu)-[:ACTED_IN {roles:['Neo']}]->(TheMatrix)
CREATE (Keanu)-[:ACTED_IN {roles:['Neo']}]->(TheMatrixReloaded)

CREATE (TomH)-[:ACTED_IN {roles:['Joe Fox']}]->(YouveGotMail);

我们可以通过运行以下查询找到所有 ACTED_IN 路径

MATCH path = ()-[:ACTED_IN]->()
RETURN path
结果
路径

(:Person {name: "Keanu Reeves", born: 1964})-[:ACTED_IN {roles: ["Neo"]}]→(:Movie {tagline: "Welcome to the Real World", title: "The Matrix", released: 1999})

(:Person {name: "Keanu Reeves", born: 1964})-[:ACTED_IN {roles: ["Neo"]}]→(:Movie {tagline: "Free your mind", title: "The Matrix Reloaded", released: 2003})

(:Person {name: "Tom Hanks", born: 1956})-[:ACTED_IN {roles: ["Joe Fox"]}]→(:Movie {tagline: "At odds in life…​ in love on-line.", title: "You’ve Got Mail", released: 1998})

如果我们想从每条路径中取出第一个元素,该怎么办?我们可以尝试这样做:

MATCH path = ()-[:ACTED_IN]->()
RETURN path[0]
结果
Type mismatch: expected List<T> but was Path (line 3, column 8 (offset: 40))
"RETURN path[0]"
        ^

我们可以使用 apoc.convert.toList 将路径转换为列表,然后从该列表中取出第一个项。

apoc.convert.toList
MATCH path = ()-[:ACTED_IN]->()
RETURN apoc.convert.toList(path)[0] AS output
使用 Cypher 的 node()、relationships() 和 reduce()
MATCH path = ()-[:ACTED_IN]->()
WITH nodes(path) AS nodes, relationships(path) AS rels
RETURN reduce(acc=[nodes[0]], i IN range(0,size(rels)-1) | acc + rels[i] + nodes[i+1])[0] AS output
结果
输出

(:Person {name: "Keanu Reeves", born: 1964})

(:Person {name: "Keanu Reeves", born: 1964})

(:Person {name: "Tom Hanks", born: 1956})

© . This site is unofficial and not affiliated with Neo4j, Inc.