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

1

6 Answers 6

195
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.

4
  • 1
    is there any different way to get data like (s:SomeLabel {id:65110}) ?
    – DonkeyKong
    Commented Mar 5, 2015 at 12:39
  • @DonkeyKong 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.
    – pvoosten
    Commented Apr 13, 2015 at 20:30
  • 37
    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 entities Commented May 20, 2015 at 15:58
  • Adding official cypher's documentation paragraph if someone interested: neo4j.com/docs/cypher-manual/current/clauses/match/… Commented Jan 20, 2019 at 21:12
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
6
  • Results from EXPLAIN and PROFILE for a simple query showed me that @Code was right. Why is this not in the docs?
    – Sonata
    Commented Dec 1, 2016 at 15:51
  • @Sonata What version are you running? Newer versions of Neo4j should make START obsolete.
    – Codebling
    Commented Dec 2, 2016 at 9:59
  • 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)
    – Sonata
    Commented Dec 2, 2016 at 15:40
  • @Sonata 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.
    – Codebling
    Commented Dec 2, 2016 at 18:52
  • all of the links are dead. Can you update them?
    – Ooker
    Commented Oct 5, 2021 at 14:06
6

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

1

Below cypher query gives you the solution:

MATCH (s:SKILLS) where n.ID contain '65110' return s
1

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
0
0

You should prefer create ID property yourself.

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

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.
    – Community Bot
    Commented Oct 11, 2023 at 1:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.