0

I want to assign id to a group of nodes (not the id() one). From this answer I can return the increment of number along with the nodes:

MATCH (n) where n.gid="Tt" 
WITH collect(n) as nodes
WITH apoc.coll.zip(nodes, range(0, size(nodes))) as pairs
UNWIND pairs as pair 
RETURN pair[0].gid as gid, pair[1] as rowNumber

╒═════╤═══════════╕
│"gid"│"rowNumber"│
╞═════╪═══════════╡
│"Tt" │0          │
├─────┼───────────┤
│"Tt" │1          │
├─────┼───────────┤
│"Tt" │2          │
├─────┼───────────┤
│"Tt" │3          │
├─────┼───────────┤

However if I change the last line to

SET pair[0].id=pair[1]

then I got this error:

Invalid input '[': expected ":" (line 5, column 9 (offset: 145))
"set pair[0].id=pair[1]"
         ^

Is there a way to set the ids of the nodes to be incrementing numbers? Surely Cypher understands pair[0] alone as nodes; and if it's in the RETURN clause then pair[0].id still works. Why does this syntax not work in SET?

Follow up question: How to set node properties as incrementing numbers, but resetting the increment when the value of a different property changes?

Ooker
  • 1,969
  • 4
  • 28
  • 58

1 Answers1

1

This should work, wrapping the pair[0] in () to tell Neo4j that it's a node:

MATCH (n) where n.gid="Tt" 
WITH collect(n) as nodes
WITH apoc.coll.zip(nodes, range(0, size(nodes))) as pairs
UNWIND pairs as pair 
SET (pair[0]).id = pair[1]
Graphileon
  • 5,275
  • 3
  • 17
  • 31
  • Do you know what kind of syntax is this? Surely Cypher understands `pair[0]` alone as nodes; and if it's in the `RETURN` clause then `pair[0].id` still works. Why does this syntax not work in `SET`? – Ooker Jan 21 '22 at 14:02
  • [How to set node properties as incrementing numbers, but resetting the increment when the value of a different property changes?](https://stackoverflow.com/q/70803132/3416774) – Ooker Jan 21 '22 at 14:46
  • 1
    I cannot tell you WHY SET does not take pair[0] . My 2cts? SET is an operation that modiefies data, so probably requirements are more strict than just RETURNing data. – Graphileon Jan 21 '22 at 15:02