Neo4j 向节点链中的节点添加增量属性

Neo4j 向节点链中的节点添加增量属性,neo4j,cypher,Neo4j,Cypher,我有一个节点链,我想向每个节点添加一个属性“DtaID”,该值应该在链中递增。有没有办法用Cypher做到这一点?我想你有一个这样格式的节点链: create (:Node)-[:LINK]->(:Node)-[:LINK]->(:Node)-[:LINK]->(:Node)-[:LINK]->(:Node)-[:LINK]->(:Node)-[:LINK]->(:Node) 然后我用了这个密码: // Get the path between the st

我有一个节点链,我想向每个节点添加一个属性
“DtaID”
,该值应该在链中递增。有没有办法用Cypher做到这一点?

我想你有一个这样格式的节点链:

create (:Node)-[:LINK]->(:Node)-[:LINK]->(:Node)-[:LINK]->(:Node)-[:LINK]->(:Node)-[:LINK]->(:Node)-[:LINK]->(:Node)
然后我用了这个密码:

// Get the path between the start and end nodes, including both
MATCH p = (start:Node)-[:LINK*]->(end:Node)
WHERE NOT (end)-[:LINK]->() AND NOT ()-[:LINK]->(start)
// extract the nodes of p and calc an array of indexes to 
// access each node in the array of nodes called `nodes`.
WITH nodes(p) as nodes, range(0, size(nodes(p))) AS indexes
// unwind indexes as index...
UNWIND indexes AS index
// set the value of `DtaID` property of each nodes to the index value.
// That is: the node 0 will have `DtaID` equal to 0.
// I'm assuming that you need an increment by one. If you need a
// different increment you can do calculations here. For example:
// SET (nodes[index]).DtaID = index * 10 to increment by 10.
SET (nodes[index]).DtaID = index
因此:

╒═══════════╕
│"n"        │
╞═══════════╡
│{"DtaID":0}│
├───────────┤
│{"DtaID":1}│
├───────────┤
│{"DtaID":2}│
├───────────┤
│{"DtaID":3}│
├───────────┤
│{"DtaID":4}│
├───────────┤
│{"DtaID":5}│
├───────────┤
│{"DtaID":6}│
└───────────┘
如果需要使用第一个节点的
DtaID
值作为基值,可以将第一个节点传递给
WITH
子句,并在计算中使用

MATCH p = (start:Node)-[:LINK*]->(end:Node)
WHERE NOT (end)-[:LINK]->() AND NOT ()-[:LINK]->(start)
WITH start, nodes(p) as nodes, range(0, size(nodes(p))) AS indexes
UNWIND indexes AS index
SET (nodes[index]).DtaID = start.DtaID + index

这对我来说太完美了。非常感谢并致以最良好的问候,Andreas@AndreasKuczera你好不客气!如果这个答案已经解决了你的问题,请通过点击复选标记来考虑。这向更广泛的社区表明,你已经找到了一个解决方案,并给回答者和你自己带来了一些声誉。没有义务这么做。谢谢你的建议,我不知道。顺致敬意,Andreas@AndreasKuczera不客气,安德烈亚斯!我建议你更好地了解StackOverflow。