Neo4j – Get node by id with cypher

Today in this article, we will see Neo4j – Get node by id with cypher query examples.

We will see how to get nodes/records using the ID of a given node.

The ID is a function that gets you the ID of a node or relationship. It does return the same value for both nodes and relationships in the same database.

Please note that this ID is different from any property called ID or ID that you create in a custom way.

As a good practice, it is recommended not to use this property for any data access or operations.

As per Neo4j guidelines,

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

Getting started

Here below is a sample schema or document we shall use for the date range query,

Neo4j get Nodes ID query – Commands with examples

MATCH (n:Movie) RETURN ID(n) limit 5

Here we will be using the above query to get the IDs of each node from the movie node label.

Neo4j Nodes using ID query – Commands with examples

Let’s now do the vice versa. We will access the nodes or relationships using the above ID.

MATCH (n:Movie) where  ID (n) = 15 return n

Above we are returning the node where ID is equal to 15

Neo4j 5 and above changes

The function id() is deprecated. It is recommended to use elementId().

Example

MATCH (n:Movie) where  elementId (n) = 15 return 

Other references:

That’s all! Happy coding!

Does this help you fix your issue?

Do you have any better solutions or suggestions? Please sound off your comments below.



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *