Search code examples
neo4jcypher

Neo4J: Renaming property keys


I just got started on Neo & tried to look for prior questions on this topic. I need help to rename one of the property keys.

I created the following node:

CREATE (Commerce:Category {title:' Commerce', Property:'Category', Owner:'Magic Pie', Manager:'Simple Simon'})

Now want to rename title to name. Is there a way to do it? I don't want to delete the node as there are 100's of nodes with the property "title".


Solution

  • Yes, you want to SET a new property name with the value of the old property title. And then REMOVE the old property title. Something like this...

    MATCH (c:Category)
    WHERE c.name IS NULL
    SET c.name = c.title
    REMOVE c.title
    

    If you have MANY nodes, it is advisable to perform the operation in smaller batches. Here is an example of limiting the operation to 10k at a time.

    MATCH (c:Category)
    WHERE c.name IS NULL
    WITH c
    LIMIT 10000
    SET c.name = c.title
    REMOVE c.title