cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Return node id as property

ri8ika
Graph Voyager

I need node id as property, how can I achieve?

RETURN { id(u): 23, value: 'value' } // ERROR

Result:

{
23: 23,
value: 'value'
}
6 REPLIES 6

This will give you the node id of the n node(s) and put them as property of the n nodes(s).
MATCH (n)
SET n.id = id(n)

That's not what I'm looking for. I wanted to return id as property not setting it.

Hello @ri8ika

You can get what you want with APOC plugin but you can only generate String key:

MATCH (n)
WHERE id(n) = 23
RETURN apoc.map.setKey({}, toString(id(n)), id(n))

OR

MATCH (n)
WHERE id(n) = 23
RETURN apoc.map.fromLists([toString(id(n)), "value"], [id(n), "value"])

Regards,
Cobra

Wow, it's cool. But I am not using apoc. Isn't there anyway to do it without apoc?

I tried:

RETURN {toString(id(n)): id(n)} // Error

I'm afraid that APOC is the only solution to do what you want.

Indeed if you are trying to do something like assigning a dynamic name to a property based on an id, Cypher is not build to do things like dynamic property name, it's meet to be used as a query language first.

APOC is really easy to install, the plugin table from Neo4j Desktop, and you have an almost but quite really complete documentation here.