Question

I have a graph in which versioning information is stored as [:ADD] or [:REMOVE] relations between nodes. I want to replace those rels by another model, based on [:UPDATE] rels with property type and timestamp.

Currently

MATCH (n:tocversion)-[r:ADD]->(m) 
RETURN n.version,id(m)

returns this (as expected)

n.version,id(m)
1,13
1,14
2,15
2,16
3,17
3,18
3,19
3,20
4,21
4,22

Now I thought I could collect the versions and m's and use them as a basis to create rels in the new model. Like this.

MATCH (n:tocversion)-[r:ADD]->(m),(t:toc) 
WITH t,COLLECT(n.version) AS versions, COLLECT(m) AS ms
FOREACH(i IN versions |
    FOREACH(m1 IN [ms[i]]|
        CREATE (t)-[r1:UPDATE {type:"ADD", version:versions[i]}]->(m1)))

However, the rels are created in a way I don't understand, because

MATCH (t:toc)-[r:`UPDATE`]->(b) RETURN r.version,r.type,id(b)

returns

r.version,r.type,id(b)
  1,      ADD,      14
  1,      ADD,      14
  2,      ADD,      15
  2,      ADD,      15
  2,      ADD,      16
  2,      ADD,      16
  2,      ADD,      16
  2,      ADD,      16
  3,      ADD,      17
  3,      ADD,      17

instead of the expected

r.version,r.type,id(b)
  1,      ADD,      13
  1,      ADD,      14
  2,      ADD,      15
  2,      ADD,      16
  3,      ADD,      17
  3,      ADD,      18
  3,      ADD,      19
  3,      ADD,      20
  4,      ADD,      21
  4,      ADD,      22
Was it helpful?

Solution

Found it. Had to use RANGE

match (n:tocversion)-[r:ADD]->(m),(t:toc) 
with t,collect(n.version) as versions, collect(m) as ms
foreach(i in RANGE(0, LENGTH(versions)-1) |
    foreach(m1 in [ms[i]]|
        create (t)-[r1:UPDATE5 {type:"ADD", version:versions[i]}]->(m1)))

OTHER TIPS

Likely because of this:

FOREACH(i IN versions |
FOREACH(m1 IN [ms[i]] |

Your "i" is going to be: 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, as expected.

But if you're using those as indices into the ms[] collection (which is 0-based), you're going to be looking at ms[] = {13, 14, 15, 16, 17, .. , 22}, and so ms[1] will always be 14, ms[2] will always be 15, ms[3] will always be 16, and ms[4] will always be 17.

Your "foreach" loops need to be rethought as "i" shouldn't be used as a lookup into "ms".

In fact I'm also not certain "i" should be used as an index into "versions" which you do in your CREATE statement as you'll likely have a similar issue as above (e.g. versions[3] will always be 2).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top