Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neo4j Cypher query with timestamp

Tags:

neo4j

cypher

I am attempting to query the db for the 10 most recently created nodes. I have attempt

MATCH (a:Post) RETURN a ORDER BY TIMESTAMP() LIMIT 10

I have also tried this

MATCH (a:Post) RETURN a ORDER BY TIMESTAMP() DESC LIMIT 10

If I create nodes with contents {one, two, three} in that order, both queries produce the nodes in the order one, two, three. Any thoughts or ideas as to why this happens??

like image 304
user3494794 Avatar asked Oct 24 '25 19:10

user3494794


1 Answers

TIMESTAMP() is a scalar function to mean the exact time at query execution. It does not have anything to do with the time the nodes or relationships were created.

That is why you get the exact same results for both queries. You're simply ordering by the current time, which doesn't make a lot of sense because the current time is exactly the same for all records.

Neo4j does not store any creation timestamps by default. You need to store them as an additional property, if it's important to you. This is where you should use the scalar function.

CREATE (:Post {created_at: TIMESTAMP()})

Once that is done, match and order like this.

MATCH (a:Post) RETURN a ORDER BY a.created_at LIMIT 10

Note that you're ordering by the created_at property, and not the TIMESTAMP() scalar function.

like image 77
ADTC Avatar answered Oct 26 '25 12:10

ADTC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!