Cypher Neo4J - CASE Expression with MERGE

后端 未结 4 1213
伪装坚强ぢ
伪装坚强ぢ 2020-11-30 11:25

I\'m trying to implement the logic in Cypher where, based on a particular condition (CASE Statement), I would create some nodes and relationships; the code is

4条回答
  •  甜味超标
    2020-11-30 12:07

    To do conditional write operations you need to use the FOREACH trick. Using CASE you either return a one element array or a empty one. FOREACH iterates over the CASE expression and therefore conditionally executes the action. If you want an ELSE part as well you need to have a another FOREACH using the inverse condition in the CASE. As an example, instead of

    WHEN 'y' THEN
       MERGE (u)-[r2:STAGE {startdate:20141225, enddate:'99999999', status:'InProgress'}]->(b2     {fork:'fail'}) RETURN 1
    ELSE 
        MERGE (u)-[r2:STAGE {startdate:20141225, enddate:'99999999', status:'InProgress'}]->(b2)   RETURN 2
    END
    

    use

    FOREACH(ignoreMe IN CASE WHEN 'y' THEN [1] ELSE [] END | 
        MERGE (u)-[r2:STAGE {startdate:20141225, enddate:'99999999', status:'InProgress'}]->(b2 {fork:'fail'})
    )
    FOREACH(ignoreMe IN CASE WHEN NOT 'y' THEN [1] ELSE [] END | 
        MERGE (u)-[r2:STAGE {startdate:20141225, enddate:'99999999', status:'InProgress'}]->(b2)
    )
    

    See also Mark's blog post on this.

提交回复
热议问题