I'm looking for a way to generate unique identifiers for all my nodes/relationships in Neo4j, based on an incrementing counter (not big long uuids).
The internal ids maintained by the Neo4j engine are known not to be reliable as outside references.
A solution that comes close is the code proposed in this question, but it doesn't work when a single CREATE
clause creates multiple new nodes:
// 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)
When there are multiple (n:something)
, every newly created (:somethingRelated)
will share the same id. Is there some way around this, using only Cypher?
SET n.id = id(n)
which is an incrementing number. But which can be reused after deleting and recreating nodes. – Michael Hunger