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

(Ahmedmelmoselhy) #1

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.


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


(Andrew Bowman) #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
1 Like

(Ahmedmelmoselhy) #3

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