How to set a conditional property value?

I am once again stuck on what I think should be a dirt-simple question that I'm unable to resolve using the Cypher manual -- how to apply a conditional value to the right-hand-side of a SET expression.

I have computed and want to retain a property value that sometimes meets some criteria and sometimes does not. I want to write a SET expression that accomplishes this feat.

Consider a property whose name is "foobarName". Suppose I want to assign a value to "foobarName" that is "0" when some computed quantity is negative and the computed quantity otherwise.

How do I say, for example, "If value < 0, SET foobarName = '0' else SET foobarName = TOSTRING(value)"?

Once again, the manual is opaque about what is and is not allowed in various contexts. Do I write some some kind of "WHERE" clause? Do I use some "CASE" expression someplace? Is the RHS of a SET pair an expression? Do I do this in some kind of "WITH ... AS ..." statement?

It seems to me that conditionally assigning a value to a property is a common use-case. What am I missing?

Try something like this:
WITH
CASE WHEN r.value < 0 THEN 0 ELSE toString(value) END as newValue
SET foobarName = newValue

1 Like

Ah, interesting. I appreciate your helpful response.

I discovered, through trial-and-error, something similar that seems to work:

WITH datapoint,
  (TOINTEGER(datapoint.cumulativeCaseCount) - TOINTEGER(priorDatapoint.cumulativeCaseCount)) AS dailyCaseCount,
  (TOINTEGER(datapoint.cumulativeDeathCount) - TOINTEGER(priorDatapoint.cumulativeDeathCount)) AS dailyDeathCount
SET
  datapoint.dailyCaseCount = CASE WHEN dailyCaseCount >= 0 THEN TOSTRING(dailyCaseCount) ELSE '0' END,
  datapoint.dailyDeathCount = CASE WHEN dailyDeathCount >= 0 THEN TOSTRING(dailyDeathCount) ELSE '0' END

I hope that somebody is working to improve the Cypher manual so that it allows technically proficient developers to work out things like this without so much experimentation.

I wonder if there is some way of crowd-sourcing supplemental documentation.

Thanks again for your helpful response.

2 Likes