0

From the answer of How to set node properties as incrementing numbers?, I can set node properties as increasing numbers:

MATCH (n) where n.gid="A" 
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]
return pair[0].gid, pair[0].id
╒═════════════╤════════════╕
│"pair[0].gid"│"pair[0].id"│
╞═════════════╪════════════╡
│"A"          │0           │
├─────────────┼────────────┤
│"A"          │1           │
├─────────────┼────────────┤
│"A"          │2           │
├─────────────┼────────────┤
│"A"          │3           │
├─────────────┼────────────┤
│"A"          │4           │
├─────────────┼────────────┤

But since I have a list of gid: ["A", "B", "C", "D", ...], and I want to run through all the nodes, and each time the gid value changes the incrementing numbers reset. So the result would be:

╒═════════════╤════════════╕
│"pair[0].gid"│"pair[0].id"│
╞═════════════╪════════════╡
│"A"          │0           │
├─────────────┼────────────┤
│"A"          │1           │
├─────────────┼────────────┤
│"A"          │2           │
├─────────────┼────────────┤
│...          │...         │
├─────────────┼────────────┤
│"A"          │15          │
├─────────────┼────────────┤
│"B"          │1           │
├─────────────┼────────────┤
│"B"          │2           │ 

I use

MATCH (p) with collect(DISTINCT p.gid) as gids
UNWIND gids as gid
MATCH (n) where n.gid=gid
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]
return pair[0].name, pair[0].id

and it doesn't reset the number, i.e.

╒═════════════╤════════════╕
│"pair[0].gid"│"pair[0].id"│
╞═════════════╪════════════╡
│"A"          │0           │
├─────────────┼────────────┤
│"A"          │1           │
├─────────────┼────────────┤
│"A"          │2           │
├─────────────┼────────────┤
│...          │...         │
├─────────────┼────────────┤
│"A"          │15          │
├─────────────┼────────────┤
│"B"          │16          │
├─────────────┼────────────┤
│"B"          │17          │ 

Why is that?

Ooker
  • 1,969
  • 4
  • 28
  • 58

1 Answers1

1

The answer to the question "Why is that?" is that your cypher only results in a single list.

I think that when you split the lists by adding a n.gid on line 4

MATCH (p) with collect(DISTINCT p.gid) as gids
UNWIND gids as gid
MATCH (n) where n.gid=gid

// <<< do a "group by"
WITH n.gid AS gid, 
     collect(n) as nodes    // <<< do a "group by"

WITH apoc.coll.zip(nodes, range(0, size(nodes))) as pairs
UNWIND pairs as pair 
SET (pair[0]).id = pair[1]
return pair[0].name, pair[0].id

it could work.

Graphileon
  • 5,275
  • 3
  • 17
  • 31
  • Thanks. But I don't get why `WITH n.gid AS gid` will do the group by but without it it won't? – Ooker Jan 21 '22 at 18:03
  • WITH in combination with a COLLECT() works like a group by in SQL – Graphileon Jan 21 '22 at 18:37
  • but in my original code there is `WITH collect(n) as nodes`? Or do you mean that normally `n.gid AS gid` doesn't make a group by, but because it's in the same line with `WITH collect(n) as nodes` then it acts like a group by? – Ooker Jan 21 '22 at 18:48
  • 1
    Yep that how it works. See also https://neo4j.com/docs/cypher-manual/current/functions/aggregating/ – Graphileon Jan 21 '22 at 20:14