如何将节点属性设置为递增数字,但在不同属性值更改时重置增量? [英] How to set node properties as incrementing numbers, but resetting the increment when the value of a different property changes?

查看:17
本文介绍了如何将节点属性设置为递增数字,但在不同属性值更改时重置增量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据How to set node properties as incrementing 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           │
├─────────────┼────────────┤
但由于我有一个<[2-0]:["A", "B", "C", "D", ...]列表,我希望遍历所有节点,并且每次gid值更改时,将重置递增的数字。因此,结果将是:

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

我使用

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

并且它不会重置数字,即

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

为什么?

推荐答案

问题的答案是您的密码只会导致单个列表。

我认为当您通过在第4行添加n.gid来拆分列表时

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆