Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
09-14-2018 12:09 PM
09-14-2018 03:47 PM
Conditionals aren't officially a feature in Cypher, but there is a trick that can be used to mimic them, if the conditional only deals with write operations (SET, MERGE, CREATE, REMOVE, DELETE) using FOREACH.
It relies on the fact that the inner FOREACH logic only executes when used on a non-empty list, so when the list has a single value, the inner Cypher will execute, and when the list is empty then it won't. We use CASE to conditionally use a single-element or empty list to control this.
Here's an example of usage:
...
FOREACH (ignoreMe in CASE WHEN <condition expression> THEN [1] ELSE [] END |
<write clauses here> )
...
However, this is basically a hack, and requires another FOREACH for each conditional (so one FOREACH for a simple if
, two for an if/else
pairing, etc), and you're restricted to write-only clauses (no ability to use MATCH or WITH), so this is limited.
The richer alternative is to use APOC Procedures, which have conditional cypher execution procedures for both if/else logic and case-based logic.
Here's the signature for it:
...
CALL apoc.when(<condition expression>, <if-query-string>, <else-query-string>, <parameter map to use by the queries>) YIELD value
...
Keep in mind the if/else queries here are executed within their own scope, and won't have access to variables in scope automatically, you need to pass them in via the parameter map. You can have the queries return values, and return them via the value
output (so if you RETURN n
, you can access this after the call with value.n
).
Also if the queries are write queries (CREATE, MERGE, SET, DELETE, REMOVE) then you'll need to use the write version of the procedure (so for this one, apoc.do.when()
).
All the sessions of the conference are now available online