apoc.cypher.runFirstColumnMany
Syntax |
|
||
Description |
Runs the given statement with the given parameters and returns the first column collected into a |
||
Arguments |
Name |
Type |
Description |
|
|
The Cypher query to execute. |
|
|
|
The parameters needed for input to the given Cypher query. |
|
Returns |
|
This function is not considered safe to run from multiple threads. It is therefore not supported by the parallel runtime (introduced in Neo4j 5.13). For more information, see the Cypher Manual → Parallel runtime. |
Using dynamic labels in Cypher
Node labels and relationship types can be referenced dynamically in Cypher without using APOC.
CREATE (n1:$(label))-[r:$(type)]->(n2:$(label))
MERGE (n1:$(label))-[r:$(type)]->(n2:$(label))
MATCH (n1:$(label))-[r:$(type)]->(n2:$(label))
The dynamically calculated type must evaluate to a STRING
or LIST<STRING>
.
For more information, see the Cypher Manual → CREATE,
MERGE, MATCH.
Usage Examples
The examples in this section are based on the following sample graph:
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);
This function is useful for executing Cypher statements that have a dynamic node label or relationship type and return multiple rows of one column. However, this can also be achieved without using APOC (when possible, the below examples will showcase both Cypher and APOC alternatives).
For example, we can return a list of all labels along with the distinct sets of property keys used by nodes with those labels.
CALL db.labels()
YIELD label
RETURN label,
apoc.cypher.runFirstColumnMany(
"MATCH (n:`" + label + "`)
WITH DISTINCT keys(n) AS keys
RETURN DISTINCT apoc.coll.sort(keys)",
{}) AS keys;
CALL db.labels()
YIELD label
RETURN label, COLLECT {
MATCH (n:$(label))
WITH DISTINCT keys(n) AS keys
RETURN DISTINCT apoc.coll.sort(keys)
};
label | keys |
---|---|
"Person" |
[["born", "name"]] |
"Movie" |
[["released", "tagline", "title"], ["released", "title"]] |