Neo4J get node by ID
Asked Answered
L

6

111

I am using neo4j for one of my project, there's a node which only has a single property as name, I want to get that node using ID, it already has a ID but when I use this code

MATCH (s:SKILLS{ID:65110}) return s

It returns nothing, heres my node

enter image description here

If the query is wrong then how do I query it using the number

Liesa answered 13/3, 2014 at 5:7 Comment(1)
Which graph visualizing tool do you use ? Thanks. I know of yworks.com/neo4j-explorer good option for customization.Emogene
M
189
MATCH (s)
WHERE ID(s) = 65110
RETURN s

The ID function gets you the id of a node or relationship. This is different from any property called id or ID that you create.

Materiel answered 13/3, 2014 at 5:22 Comment(4)
is there any different way to get data like (s:SomeLabel {id:65110}) ?Pesach
@Pesach No, because you can also add the id property, which is something else than the id. An id property can have any type, while the node or edge id is an unsigned integer, linked to a location in the internal structure of Neo4J.Menorca
STANDARD DISCLAIMER: Don't use internal Neo4j IDs for long-term entity identification. Future versions of Neo4j might shift these IDs around for performance purposes. Create your own unique ID property (ideally with a CONSTRAINT) for tracking entitiesNuke
Adding official cypher's documentation paragraph if someone interested: neo4j.com/docs/cypher-manual/current/clauses/match/…Parallelism
M
21

START should only be used when accessing legacy indexes. It is disabled in Cypher 2.2 3.2 and up.

Neo4j recommends using WHERE ID(n) = , and furthermore states that it will only require a single lookup (does not scan every node to find the matching ID)

WARNING
The answer below is incorrect. It is listed for reference only. See the updated answer above (quoted). The text below is kept for reference only

You can use WHERE ID(s) = 65110, but this will check the ID of every node in your database.

There is a more efficient way to do this:

START s=NODE(517) MATCH(s) RETURN s
Macrae answered 26/2, 2016 at 20:26 Comment(6)
Results from EXPLAIN and PROFILE for a simple query showed me that @Code was right. Why is this not in the docs?Adriel
@Adriel What version are you running? Newer versions of Neo4j should make START obsolete.Macrae
3.0.7. Have a look at the Result Details from these examples in the console: console.neo4j.org/r/dbz1we (doing an AllNodesScan) and console.neo4j.org/r/9076wd (doing a NodeById)Adriel
@Adriel I'm not sure why this is happening. First of all, it shouldn't work - docs state that START is deprecated as of Cypher 2.0 and disabled as of Cypher 2.2, but it's clearly still working. Second of all, MATCH with ID should be a +NodeByIdSeek accessing only 1 node but for some reason it is doing an +AllNodesScan.Macrae
all of the links are dead. Can you update them?Suffragist
@Suffragist thanks for letting me know. Please note I did not have any updated links and had to archived links from archive.org. Anyone is free to do the same and update an outdated answer.Macrae
B
5

you can say:

(n:User) where id(n) >=20 RETURN n

this will return all nodes of type User with node reference ID more than 20

Blitzkrieg answered 20/5, 2015 at 15:18 Comment(0)
P
1

Below cypher query gives you the solution:

MATCH (s:SKILLS) where n.ID contain '65110' return s
Polymerism answered 19/4, 2023 at 15:21 Comment(0)
S
0

You should prefer create ID property yourself.

Using system created ID is not advisable and should be avoided, for best practice.

Sabotage answered 3/10, 2023 at 11:20 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Helmholtz
B
0

Starting with version 5, the function id() has been deprecated. To achieve the same result, you need to use new function elementId

MATCH (n)
WHERE elementId(n) = $id
RETURN n.name
Bavardage answered 15/12, 2023 at 19:33 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.