neo4j - Neo4j 中所有节点/关系的可靠(自动)递增标识符

标签 neo4j cypher uniqueidentifier

我正在寻找一种基于递增计数器(不是大而长的 uuid)为 Neo4j 中的所有节点/关系生成唯一标识符的方法。

众所周知,由 Neo4j 引擎维护的内部 ID 作为外部引用并不可靠。

接近的解决方案是 the code proposed in this question , 但当单个 CREATE 子句创建多个新节点时它不起作用:

// get unique id
MERGE (id:UniqueId{name:'Person'})
ON CREATE SET id.count = 1
ON MATCH SET id.count = id.count + 1
WITH id.count AS uid
// create a new node attached to every existing :something node
MATCH (n:something)
CREATE (:somethingRelated {id:uid}) -[:rel]-> (n)

当有多个 (n:something) 时,每个新创建的 (:somethingRelated) 将共享相同的 id。有没有办法解决这个问题,只使用 Cypher?

最佳答案

试试这个来分配一个 id block :

// collect nodes to connect
MATCH (n:Crew) WITH collect(n) AS nodes
MERGE (id:UniqueId { name:'Person' })
// reserve id-range
SET id.count = coalesce(id.count,0)+ size(nodes)
WITH nodes, id.count - size(nodes) AS base 
// for each index
UNWIND range(0,size(nodes)-1) AS idx
// get node, compute id
WITH nodes[idx] AS n, base +idx AS id
CREATE (:SomethingRelated { uid:id })-[:rel]->(n)

关于neo4j - Neo4j 中所有节点/关系的可靠(自动)递增标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32040409/

相关文章:

ios - 在 neo4j 支持的 iOS 应用程序上创建节点时分配 UUID

neo4j - 通过遍历进行分组和排序

neo4j - 使用spring data neo4j时如何通过相关对象id获取实体?

neo4j - 不区分大小写的标签搜索

java - 使用 System.currentTimeMillis() 生成唯一的数据库 ID 安全吗?

ios - 如何验证 IDFV (idenitifierForVendor) 的供应商?

c - C 中标识符长度的限制

neo4j - neo4j 中的高度节点

neo4j - Cypher 中没有线性图形模式

neo4j - 在 neo4j 中为数组中的每个元素创建关系