110

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

Niraj Chauhan
  • 7,677
  • 12
  • 46
  • 78

4 Answers4

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

jjaderberg
  • 9,844
  • 34
  • 34
Damon Horrell
  • 2,054
  • 1
  • 13
  • 9
  • 1
    is there any different way to get data like (s:SomeLabel {id:65110}) ? – DonkeyKong Mar 05 '15 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 Apr 13 '15 at 20:30
  • 35
    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 – Brian Underwood May 20 '15 at 15:58
  • Adding official cypher's documentation paragraph if someone interested: https://neo4j.com/docs/cypher-manual/current/clauses/match/#get-node-rel-by-id – Ziemowit Stolarczyk Jan 20 '19 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
Codebling
  • 10,764
  • 2
  • 38
  • 66
  • Results from EXPLAIN and PROFILE for a simple query showed me that @Code was right. Why is this not in the docs? – Sonata Dec 01 '16 at 15:51
  • @Sonata What version are you running? Newer versions of Neo4j should make START obsolete. – Codebling Dec 02 '16 at 09:59
  • 3.0.7. Have a look at the `Result Details` from these examples in the console: http://console.neo4j.org/r/dbz1we (doing an AllNodesScan) and http://console.neo4j.org/r/9076wd (doing a NodeById) – Sonata Dec 02 '16 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](http://neo4j.com/docs/stable/execution-plans-starting-query.html#query-plan-node-by-id-seek) but for some reason it is doing an `+AllNodesScan`. – Codebling Dec 02 '16 at 18:52
  • all of the links are dead. Can you update them? – Ooker Oct 05 '21 at 14:06
  • @Ooker 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. – Codebling Oct 05 '21 at 19:02
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

Wesam Na
  • 2,364
  • 26
  • 23
1

Below cypher query gives you the solution:

MATCH (s:SKILLS) where n.ID contain '65110' return s
Tono Kuriakose
  • 718
  • 6
  • 19