简体   繁体   中英

Cypher - conditional where clause

I am trying to update a users subject name either by id or by name. Currently the query I have is this:

 MATCH (s:Subject)-[:REGISTERED-BY]->(u:User{id: 1})
 WHERE s.name="Math" or s.id=""
 SET s.name = "Maths"
 RETURN s

If I only use the name without OR , it works. If I only use ID and leave name as an empty string it works. But if I use the name and leave id empty in the OR clause. The update doesn't happen I need to conditionally update the names, depending on whether the name is available or the id. The id field is a UUID. Is there a way to write that logic in the query itself?

This may help you, In Neo4j you can perform conditional operations using two options.

If you have access to APOC, then you can use APOC.do.when :

MATCH (s:Subject)-[:REGISTERED-BY]->(u:User{id: 1})

WHERE ...

CALL apoc.do.when(s.name="Math", "SET s.name = \"Maths\"", "", {}) YIELD value as _

RETURN s

The second is a more native FOREACH :

MATCH (s:Subject)-[:REGISTERED-BY]->(u:User{id: 1})

WHERE ...

FOREACH(_ IN CASE s.name WHEN "Math" THEN [1] ELSE [] END | SET s.name = "Maths" )

RETURN s

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM