Search code examples
neo4jpropertiescontains

Neo4j - Replace multiple values in string list property with single value


I have a node, with a list property. The list can contain multiple similar values:

["something one", "something two", "another property", "something three"]

My goal is to find all of the nodes that have one or more 'something ...' value(s) in the property and replace all of the 'something ...' properties with the single property value 'something'.

["something", "another property"]

As I see it there are two problems:

  1. Locate all of the nodes where the list property contains 'something'.
  2. Replace all single or multiple instances of 'something ...' with the single 'something' value.

Solution

  • This logic may work for you:

    MATCH (n:Foo)
    WHERE ANY(x IN n.list WHERE x STARTS WITH 'something')
    SET n.list =
      REDUCE(s = ['something'], t IN n.list | CASE WHEN t STARTS WITH 'something' THEN s ELSE s + t END )
    

    It uses the REDUCE function to create a list with 'something' as the first element and without any other 'something...' elements.