|| apoc.cypher.runFirstColumnSingle - APOC 核心文档 - Neo4j 文档

apoc.cypher.runFirstColumnSingle

详情

语法

apoc.cypher.runFirstColumnSingle(statement, params)

描述

运行给定语句并传入给定参数,返回第一列的第一个元素。

参数

名称

类型

描述

statement

STRING

要执行的 Cypher 查询。

params

MAP

给定 Cypher 查询所需的输入参数。

返回

ANY

此函数不被认为是线程安全的。因此,并行运行时不支持此函数。更多信息请参阅 Cypher 手册 → 并行运行时

在 Cypher 中使用动态标签

节点标签和关系类型可以在 Cypher 中动态引用,而无需使用 APOC。

动态创建、匹配和合并标签与类型的 Cypher 语法
CREATE (n1:$(label))-[r:$(type)]->(n2:$(label))
MERGE (n1:$(label))-[r:$(type)]->(n2:$(label))
MATCH (n1:$(label))-[r:$(type)]->(n2:$(label))

动态计算的类型必须解析为 STRINGLIST<STRING>。更多信息请参阅 Cypher 手册 → CREATEMERGEMATCH

使用示例

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

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

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 (TheMatrixRevolutions:Movie {title:'The Matrix Revolutions', released:2003, tagline:'Everything that has a beginning has an end'})
CREATE (SomethingsGottaGive:Movie {title:"Something's Gotta Give", released:2003})
CREATE (TheDevilsAdvocate:Movie {title:"The Devil's Advocate", released:1997, tagline:'Evil has its winning ways'})

CREATE (YouveGotMail:Movie {title:"You've Got Mail", released:1998, tagline:'At odds in life... in love on-line.'})
CREATE (SleeplessInSeattle:Movie {title:'Sleepless in Seattle', released:1993, tagline:'What if someone you never met, someone you never saw, someone you never knew was the only someone for you?'})
CREATE (ThatThingYouDo:Movie {title:'That Thing You Do', released:1996, tagline:'In every life there comes a time when that thing you dream becomes that thing you do'})
CREATE (CloudAtlas:Movie {title:'Cloud Atlas', released:2012, tagline:'Everything is connected'})

CREATE (Keanu)-[:ACTED_IN {roles:['Neo']}]->(TheMatrix)
CREATE (Keanu)-[:ACTED_IN {roles:['Neo']}]->(TheMatrixReloaded)
CREATE (Keanu)-[:ACTED_IN {roles:['Neo']}]->(TheMatrixRevolutions)
CREATE (Keanu)-[:ACTED_IN {roles:['Julian Mercer']}]->(SomethingsGottaGive)
CREATE (Keanu)-[:ACTED_IN {roles:['Kevin Lomax']}]->(TheDevilsAdvocate)

CREATE (TomH)-[:ACTED_IN {roles:['Joe Fox']}]->(YouveGotMail)
CREATE (TomH)-[:ACTED_IN {roles:['Sam Baldwin']}]->(SleeplessInSeattle)
CREATE (TomH)-[:ACTED_IN {roles:['Mr. White']}]->(ThatThingYouDo)
CREATE (TomH)-[:ACTED_IN {roles:['Zachry', 'Dr. Henry Goose', 'Isaac Sachs', 'Dermot Hoggins']}]->(CloudAtlas)
CREATE (TomT)-[:DIRECTED]->(CloudAtlas);

此函数适用于执行具有动态节点标签或关系类型且返回一行一列的 Cypher 语句。但是,这也可以在不使用 APOC 的情况下实现(如果可能,下面的示例将展示 Cypher 和 APOC 的替代方法)。

例如,我们可以通过运行以下查询来返回所有标签及其计数的对象:

apoc.cypher.runFirstColumnSingle
CALL db.labels()
YIELD label
RETURN label,
       apoc.cypher.runFirstColumnSingle("MATCH (:" + label + ") RETURN count(*)", {}) AS count;
MATCH 子句
CALL db.labels()
YIELD label
MATCH (n:$(label))
RETURN label, count(n);
结果
标签 计数

"Person"

3

"Movie"

9

我们可以通过运行以下查询来返回所有关系类型的计数:

apoc.cypher.runFirstColumnSingle
CALL db.relationshipTypes()
YIELD relationshipType
RETURN relationshipType,
       apoc.cypher.runFirstColumnSingle("MATCH ()-[:" + relationshipType + "]->() RETURN count(*)", {}) AS count;
MATCH 子句
CALL db.relationshipTypes()
YIELD relationshipType
MATCH ()-[r:$(relationshipType)]->()
RETURN relationshipType, count(r) AS count;
结果
关系类型 计数

"ACTED_IN"

9

"DIRECTED"

1

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