Welcome to OGeek Q&A 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...