30

I'm using neo4j and making executing this query:

MATCH (n:Person) RETURN n.name LIMIT 5

I'm getting the names but i need the ids too. Please help!

Aleksandrenko
  • 2,987
  • 6
  • 28
  • 34

3 Answers3

59

Since ID isn't a property, it's returned using the ID function.

MATCH (n:Person) RETURN ID(n) LIMIT 5
subvertallchris
  • 5,282
  • 2
  • 25
  • 43
  • 1
    thanks for the fast response :) (it's the correct one) – Aleksandrenko Oct 05 '14 at 14:36
  • +subvertallchris is there a way to map the returner values in hash map, because the response this way is just an array; now: [4, 'George'] i want it: { id: 4, name: 'George' } – Aleksandrenko Oct 05 '14 at 15:13
  • 4
    You'd do it just like that, actually. `MATCH (n:Person) RETURN { id: ID(n), name: n.name } as user LIMIT 5` – subvertallchris Oct 05 '14 at 16:05
  • 1
    @subvertallchris This isn't useful if the node has a hundred properties or properties unknown at query time (because there is no schema). Is there a way to return a node with an extra computed property, but **without** reconstructing the entire node as a node literal in the RETURN clause? – Szczepan Hołyszewski Dec 23 '15 at 02:29
  • People be **CAUTIOUS** here! You should NEVER use the node of an id as a reference in some 3rd party DBs. If the node gets deleted, next created node reuses the old ID value of a node you just deleted (because it's free now). This leaves you with a **dangling reference**. Read more here (https://neo4j.com/blog/dark-side-neo4j-worst-practices) – DurkoMatko Aug 18 '19 at 11:10
3

Not sure how helpful or relevant this is, but when I'm using the NodeJS API the record objects returned from Cypher queries have an identity field on the same level as the properties object (e.g record.get(0).properties, record.get(0).identity). I'm assuming you aren't just doing plain Cypher queries and actually using a driver to send the queries - so you might not have to run another MATCH statement.

I'm aware that the OP is asking about Cypher specifically - but it might be helpful to other users that stumble upon this question.

e.upton
  • 62
  • 4
0

Or you can take a look on the Neo4j Cypher Refcard

You can get a short look to a lots of functions and patterns you can write.

And more about functions on The Neo4j Developer Manual - Chapter 3. Cypher - 3.4. Functions

Marcus.D
  • 679
  • 6
  • 7