1
votes

I need a query that checks if a node exists, a specific attribute on that node exists and post-process the result using case or something.

Ex:

CALL apoc.cypher.run("OPTIONAL MATCH (n:Location{SureName:'9144735079d813886326'}) RETURN CASE n.SubType WHEN null THEN 'Location was not loaded' ELSE n.SubType END AS result UNION OPTIONAL MATCH (n:Location{SubType:'Site',SureName:'914473507981388d6326'}) RETURN CASE n.SubType WHEN null THEN 'NotLoaded' ELSE n.SubType END as result", null) YIELD value AS rv

But I want to be able to return just one string depending on the results that I get.

Thanks.

1
Do the matches correspond with at most a single node each? Also you said that you want a single string for the results, can you provide an example of the desired output? So far it looks to me that UNION isn't needed here, but more info is needed before I can provide a full answer. - InverseFalcon
The matches correspond to a single node. The use case is like this: I have a value for a specific attribute on that node. I want to check if the node exists if the attribute exists and if the value loaded in neo4j is different than the one that I have. Depending on the result I want to return a single string. EX: If the attribute doesn't exist - "The attribute was not loaded into neo4j". Thanks. - Catalin
I think one thing that's throwing me off is the SureName properties are different for each of those optional matches. Can I assume that these are meant to be identical? - InverseFalcon

1 Answers

1
votes

You should be able to use a CASE statement to output the value based upon conditions. Maybe a query like this might work:

// assume you've passed in $expectedSubType as a parameter
OPTIONAL MATCH (n:Location{SureName:'9144735079d813886326'}) 
WITH n, n IS NULL as notLoaded
RETURN CASE WHEN notLoaded THEN 'Location was not loaded' 
            WHEN n.SubType IS NULL THEN 'SubType missing'
            WHEN n.SubType = $expectedSubType THEN 'Expected value'
            ELSE 'Different value: ' + n.SubType END AS result