How to return a specific string instead of (no changes, no records) in Cypher query

Viewed 354

my query below returns "Success" string if node n successfully matched. In case it was not matched, the output is "(no changes, no records)" while I expect the query to return "Failure" string. I guess this is to do with node n not existing and therefore 'with value.rslt' is also not existing which returns nothing. How can I get "Failure" return in case node n not matched? Thank you

match(n:device) where n.nid = 'non-existing'
CALL apoc.do.case(
[
 n is not null, "return 'Success' as rslt"
], 
"return 'Failure' as rslt",
{n:n}
) yield value
with  value.rslt  as rslt 
return rslt
1 Answers

You can do OPTIONAL MATCH, it returns NULL when there are no nodes matching the condition.

OPTIONAL MATCH (n:device) 
WHERE n.nid = 'non-existing'
RETURN DISTINCT CASE n WHEN NULL THEN 'Failure' ELSE 'Success' END
Related