Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get last node created in neo4j?

Tags:

neo4j

So I know when you created nodes neo4j has a UUID for each node. I know you can access a particular node by that UUID by accessing the ID. For example:

START n=node(144) RETURN n;

How would I get the last node that was created? I know I could show all nodes and then run the same command in anotehr query with the corresponding ID, but is there a way to do this quickly? Can I order nodes by id and limit by 1? Is there a simpler way? Either way I have not figured out how to do so through a simple cypher query.

like image 739
WildBill Avatar asked Mar 18 '23 15:03

WildBill


1 Answers

Every time not guaranteed that a new node always has a larger id than all previously created nodes,

So Better way is to set created_at property which stores current time-stamp while creating node. You can use timestamp() function to store current time stamp

Then,

Match (n)
Return n
Order by n.created_at desc
Limit 1
like image 119
Satish Shinde Avatar answered May 03 '23 19:05

Satish Shinde