Return node id as property

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

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

Result:

{
23: 23,
value: 'value'
}

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 :slight_smile:

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

1 Like

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.