Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-generation of id in Neo4j

Tags:

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?

like image 828
Jeevika Avatar asked Dec 02 '22 12:12

Jeevika


2 Answers

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
like image 60
pwsaker Avatar answered Dec 09 '22 07:12

pwsaker


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"
like image 43
Christophe Willemsen Avatar answered Dec 09 '22 07:12

Christophe Willemsen