Graphileon
Graphileon

Reputation: 5385

FOREACH with collection in cypher

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 ?

Upvotes: 2

Views: 11812

Answers (2)

Robert Brisita
Robert Brisita

Reputation: 5844

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" )

Upvotes: 5

tstorms
tstorms

Reputation: 5001

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)

Upvotes: 4

Related Questions