Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate through collection with index in Cypher

Tags:

neo4j

cypher

Say I have matched a collection of relationships:

MATCH a-[r:BELONGS_TO]->b

How can I iterate through each relationship and assign it an index? In pseudocode:

for i in range(0, # of r's)
  r.order = i
like image 453
soundly_typed Avatar asked Nov 30 '15 18:11

soundly_typed


2 Answers

This should work:

MATCH (a)-[r:BELONGS_TO]->(b)
WITH collect(r) as rels
WITH rels, range(0, size(rels)) AS is
UNWIND is AS i
WITH rels[i] as rel, i
SET rel.order = i
like image 190
William Lyon Avatar answered Nov 15 '22 11:11

William Lyon


You can hack it a bit :

MATCH (a)-[r:BELONGS_TO]->(b)
WITH collect(r) as relationships
UNWIND range(0, size(relationships)-1) as x
RETURN relationships[x]
like image 35
Christophe Willemsen Avatar answered Nov 15 '22 12:11

Christophe Willemsen