Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating through a collection with MATCH and CREATE clauses

I want to do something like this in cypher:

MATCH (n:node) WHERE n.ID = x        //x is an integer value
FOREACH (num in n.IDs: 
    MATCH (p:node) WHERE p.ID = num
    CREATE (n)-[:LINK]->(p) )

where num is an array of integer values referring to the IDs of nodes that need to be linked to the node matched in the first line.

When I run this query, I get the error: Invalid use of MATCH inside FOREACH.

I'm in the early stages of teaching myself both Cypher and Neo4j. How can I achieve my desired functionality here? Or am I barking up the wrong tree - am I failing to grasp something that makes it unnecessary for me to do so?

like image 266
drew moore Avatar asked Jan 09 '14 10:01

drew moore


1 Answers

This is not allowed, instead use the top-level MATCH like http://gist.neo4j.org/?8332363

MATCH (n:node), (p:node)
WHERE n.ID = 1 AND p.ID in [2,3,4]
CREATE (n)-[:LINK]->(p)
like image 184
Peter Neubauer Avatar answered Oct 16 '22 08:10

Peter Neubauer