Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

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

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
question from:https://stackoverflow.com/questions/65904123/how-to-return-a-specific-string-instead-of-no-changes-no-records-in-cypher-qu

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...