FOREACH with collection in cypher

10,822

Solution 1

Anyone encountering a subset error like mentioned by OP can resolve it with parentheses:

MATCH (u:user)-[i:INTEREST]->(t:term)
WITH COLLECT([i,t]) AS its
FOREACH (it IN its |
     SET (it[0]).testprop=89292" )

Solution 2

There's no need to collect the term nodes as well. Just do it as follows:

MATCH path=(u:user)-[i:INTEREST]->(t:term)
FOREACH (n IN rels(path) | set n.testprop=89292)
Share:
10,822
Graphileon
Author by

Graphileon

Graphileon helps business consultants and information analysts to rapidly design and deploy graph-based applications by exploiting the agility of graphs.

Updated on September 08, 2022

Comments

  • Graphileon
    Graphileon over 1 year

    I have a collection of rels,created using this

    MATCH (u:user)-[i:INTEREST]->(t:term)
    WITH COLLECT([i,t]) AS its
    RETURN its
    

    and it returns the array of rels and nodes correctly. see also http://console.neo4j.org/r/cw7saq

    Now I want to set the properties of the relationship, but don't see how I can access the rels in the array. Tried this,

    MATCH (u:user)-[i:INTEREST]->(t:term)
    WITH COLLECT([i,t]) AS its
    FOREACH (it IN its |
             SET it[0].testprop=89292" )
    

    but it returns an error

    Error: Invalid input '[': expected an identifier character, node labels, a property map, a relationship pattern, '(', '.' or '=' (line 4, column 16)
    "         SET it[0].testprop=89292" )"
    

    anyone knows what is the right syntax to do this ?

  • Graphileon
    Graphileon about 10 years
    Thanks @tstorms. Accepting your answer since it is correct in the given context. However the real reason why I'm collecting the term nodes as well is explained here stackoverflow.com/questions/22482138/…
  • tstorms
    tstorms about 10 years
    Thanks, I've answered the other question as well. Feel free to upvote. ;-)