Search code examples
neo4jcypherlowercase

Convert all values of all properties to lower-case in Neo4j


I have various nodes with different labels of the form:

(n:Label)
n.Name='ABS'
n.sample='ajx'

I want to change all the attribute values to lower-case. ie the result for the above should be:

(n:Label)
n.Name='abs'
n.sample='ajx'

I have tried the following...

match(n:Label) SET n.Name`=toLower(n.Name) 

But this query only updates attributes one at a time. Is there a way to change all attributes simultaneously in a single query.


Solution

  • You can do it with the help of APOC procedures. Specifically apoc.create.setProperty procedure. This query should work:

    MATCH (n)
    WITH n, [x IN keys(n) 
            WHERE n[x] =~ '.*'
    ] as props
    UNWIND props as p
    CALL apoc.create.setProperty(n, p, toLower(n[p])) YIELD node
    RETURN node
    

    This query match all nodes of your graph and get the string properties of each node. After it apoc.create.setProperty is called for each property passing as new value toLower(node[property]).