How to use If Else condition in neo4j?

Hello Team,

Greetings!!

I am looking for a sample query which uses " if and else " logic in neo4j.
My neo4j version is 3.5
Please let me know if anyone is having information related to this.

Thanks in advance!

Best Regards
Akshat

although more details may be needed a case statement will achieve some level of if then else.

But also depending on what you are trying to achieve and if you are looking to only invoke certain cypher based upon a condition you can try similar to

FOREACH ( ignoreMe in CASE WHEN n.age>20 THEN [1] ELSE [] END | merge (n) set n.status='deleted' );

which will test a property age and see if it has a value greater than 20, if it does it defines 1 element in the array which is then used by the FOREACH to run the merge 1 time. If the property age however is less than 20 then there are no elements in the array which thus results in the FOREACH not calling the merge;

Hello Mate ,

Thanks for entertaining my query.

I tried to run your given query in my system with little modification but it failed.
May be I am doing some silly mistake.

Below is my query:
FOREACH ( ignoreMe in CASE WHEN n.Hostname='jmngd1bcw170v03' THEN [1] ELSE END | merge (n:OMI) set n.status='Active' );

Error :

Neo.ClientError.Statement.SyntaxError: Variable n not defined (line 1, column 33 (offset: 32))
"FOREACH ( ignoreMe in CASE WHEN n.Hostname='jmngd1bcw170v03' THEN [1] ELSE END | merge (n:OMI) set n.status='Active' );"
^

Can you please help ?

Best Regards
Akshat

in the example it is checking on the existence of a property, in your case Hostname and against a node 'n', but you have failed to define n. You might want to consider

match (n:IP_ADDRESS) with n FOREACH ( ignoreMe in CASE WHEN n.Hostname='jmngd1bcw170v03' THEN [1] ELSE [] END | merge (n:OMI) set n.status='Active' )

which will find all nodes with a label of :IP_ADDRESS alias these nodes to the letter 'n' and then run the FOREACH against.

Additionally your original statement had a ELSE END which I suspect is better written to be ELSE [] END