Jeevika
Jeevika

Reputation: 142

Auto-generation of id in Neo4j

Will Neo4j auto-generate unique ids for all the nodes created using 'CREATE' query, as 'id' in Mysql? We created a node with

CREATE (n: user{name:"x", age:10}) return n

If we want to update 'name' property, how to do it in Neo4j?

Upvotes: 4

Views: 6616

Answers (2)

pwsaker
pwsaker

Reputation: 168

From the documentation though,

Searching for nodes by id can be done with the id() function in a predicate.

Neo4j reuses its internal ids when nodes and relationships are deleted. This means that applications using, and relying on internal Neo4j ids, are brittle or at risk of making mistakes. It is therefor recommended to rather use application generated ids.

It is a good idea to as they say, use Application generated ids that are stored as properties of the node. You need to set the application generated id while creating the node and then use that key on merge statements

MERGE (n:user{key:<myApplicationKey>}) return n
SET n.name="x", age=10

Upvotes: 13

Christophe Willemsen
Christophe Willemsen

Reputation: 20175

There is an internal id, you can access it the id() function

CREATE (n:User {name="x", age: 10}) RETURN n, id(n) as nodeId

Later on, you can update it with

MATCH (n) WHERE id(n) = 12345 SET n.name = "y"

Upvotes: 5

Related Questions