SET n.(CASE END)

Hello,

I am trying to SET a property conditional on another property.

MATCH
( n: Person )
SET
n.( CASE
WHEN n.age > 60 THEN 'old'
WHEN 60 > n.age > 30 THEN 'middle'
ELSE 'young'
END ) = true

I receive the Neo.ClientError.Statement.SyntaxError

What is syntactically wrong in my query?
How could this result be achieved another way?

You may consider having one property called something like β€˜ageRange’ and set it to young , middle , or old.

Try the following syntax.

MATCH ( n: Person )
WITH n, 
CASE
WHEN n.age > 60 THEN 'old'
WHEN 60 > n.age > 30 THEN 'middle'
ELSE 'young'
END  as property
SET n[property] = true

Hi Gary,

using your syntax I receive:

Neo.ClientError.Statement.SyntaxError
Invalid input '[': expected ":" (line 8, column 6 (offset: 167))
"SET n[property] = true"

What can be causing it?

In this exercise, setting one of properties (conditional on another property) to something is a goal in itself, so I don't consider using just one having all values.

My mistake. The error made me remember that you can't set a parameter dynamically with the bracket syntax. Here is an approach using apoc.

MATCH ( n: Person )
WITH n, 
CASE
WHEN n.age > 60 THEN 'old'
WHEN 60 > n.age > 30 THEN 'middle'
ELSE 'young'
END  as property
call apoc.cypher.doIt("set n." + property + " = true", {n:n}) yield value
return value

It might be even better to set it as a label.
You can use apoc.create.addLabel(node,[labels]) for that.
Which then also allows for more efficient matching.

MATCH
( n: Person )
call apoc.create.addLabels(n, [
CASE
WHEN n.age > 60 THEN 'old'
WHEN 60 > n.age > 30 THEN 'middle'
ELSE 'young'
END]) yield node
return count(*);
1 Like

Thanks Gary, thanks Michael,

both methods work and I learnt something new.

Nevertheless I wonder why using apoc.cypher.doIt() that incorporates SET clause, even though properties have been set, I receive "No changes, no records" message? I would expect "Set x properties" in return.

I suspect it is because the SET operation is occurring on the backend via the apoc procedure, so the executor is not aware of the impact of the 'doit' cypher statement.