Search code examples
memgraphdb

How can I update or add value to a property only if one is not set yet?


I have a list of countries. I need to assign at least one language to a country if one is not already set. If one is set nothing shouldn't be done. How can I achieve this?


Solution

  • If you want to set a property only where one doesn't exist it should be checked against IS NULL.

    MATCH (c:Country)
    WHERE c.name = "Germany" AND c.language IS NULL
    SET c.language = "German";
    

    Since the WHERE clause contains the statement c.language IS NULL, the node will only be matched if it doesn’t have a language property.