Search code examples
dictionaryneo4jcypher

neo4j return map of properties+ID


I want to get back a map (dictionary) of the properties of my nodes in cypher along with the ID of the nodes. I have the following code that does not work:

match (n:person) 
with n limit 5
with  properties(n)+oldID:ID(n) as info
return info

This does not create a single dictionary with the properties and the ID. I have tried different formats but nothing seems to work. What am I doing wrong here?


Solution

  • You can return the properties along with the id like this :

    MATCH (n:Person)
    RETURN n{.*, oldID: id(n)} AS info
    LIMIT 5
    

    Result :

    ╒═══════════════════════════════════════════════════╕
    │"info"                                             │
    ╞═══════════════════════════════════════════════════╡
    │{"born":1964,"name":"Keanu Reeves","oldID":1}      │
    ├───────────────────────────────────────────────────┤
    │{"born":1967,"name":"Carrie-Anne Moss","oldID":2}  │
    ├───────────────────────────────────────────────────┤
    │{"born":1961,"name":"Laurence Fishburne","oldID":3}│
    ├───────────────────────────────────────────────────┤
    │{"born":1960,"name":"Hugo Weaving","oldID":4}      │
    ├───────────────────────────────────────────────────┤
    │{"born":1967,"name":"Lilly Wachowski","oldID":5}   │
    └───────────────────────────────────────────────────┘
    

    Useful resource for learning Map projections https://neo4j.com/developer-blog/a-comprehensive-guide-to-cypher-map-projection/