cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to assign variable property value to nodes using apoc.periodic.iterate?

Hi, my graph contains 31 node of label "addc" and I want to add to each one of them a variable property value, here "NEw",
I'v tried the follwing code but this resulted in assigning the same (last) value (31) to all of them ( as in the figure below)

How can I make it variable?
Also, is there any other solutions, other than apoc?
Note: If it can be done via python driver, that's will be helpful also.

Thanks.

call apoc.periodic.iterate(
"UNWIND range (1,31) as i return i ",
"match (n:addc) set n.NEw=i",
{ })

1 ACCEPTED SOLUTION

You only have 31 nodes to alter here, you do not need APOC for this (you would use apoc.periodic.iterate() for batching changes when you're dealing with 10k to 100k or more entries).

The reason your approach isn't working (APOC aside) is that the match in here (match (n:addc) set n.NEw=i) is matching to all :addc nodes and applying the set to all of them, so the values will keep being overridden and the last one to apply will win.

Instead, match to your nodes, collect them, then unwind the index (from 0 to the size() -1), project out the node for the index, then do your set:

MATCH (n:addc)
WITH collect(n) as nodes
UNWIND range(0, size(nodes) -1) as index
WITH nodes[index] as node, index
SET node.NEw = index

View solution in original post

2 REPLIES 2

You only have 31 nodes to alter here, you do not need APOC for this (you would use apoc.periodic.iterate() for batching changes when you're dealing with 10k to 100k or more entries).

The reason your approach isn't working (APOC aside) is that the match in here (match (n:addc) set n.NEw=i) is matching to all :addc nodes and applying the set to all of them, so the values will keep being overridden and the last one to apply will win.

Instead, match to your nodes, collect them, then unwind the index (from 0 to the size() -1), project out the node for the index, then do your set:

MATCH (n:addc)
WITH collect(n) as nodes
UNWIND range(0, size(nodes) -1) as index
WITH nodes[index] as node, index
SET node.NEw = index

Wow, this piece of code will solve the problem. Thanks a lot 🙂
Ya, it seems like I didn't understand APOC well. nice illustration, it will help later
Thanks again 🙂