Balael
Balael

Reputation: 213

How to set more than one property depending on conditions?

thanks to cybersam I solved my problem with setting a property based on a condition:

MERGE (u:User {uuid: {userUUID}}) -[r:relation {rType: {rType}}]-> (n:Node)
ON CREATE SET 
    r.uuid = {relUUID},
    r.status = {relStatus}
ON MATCH SET
r.status = CASE WHEN r.status = "1" THEN {relStatus} ELSE r.status END
RETURN r;

Since this morning I struggle though as I wanted to set two or more conditions in this query. All trying lead to some really wierd constructions but I couldnt find the right syntax. I tried

ON MATCH SET
r.status = CASE WHEN r.status = "1" THEN {relStatus} ELSE r.status END,
r.type = CASE WHEN r.status = "1" THEN {relType} ELSE r.type END
RETURN r;

but that seemed to be to easy. Than I tried to set the properties directly

ON MATCH SET
CASE 
    WHEN r.status = "1" THEN [r.status = {relStatus}, r.type = {relType}]  
END

or to set them after the CASE

ON MATCH 
CASE 
    WHEN r.status = "1" THEN {relStatus} ELSE r.status AS queryStatus,
    WHEN r.status = "1" THEN {relType} ELSE r.type AS queryType
END
SET
r.status = queryStatus,
r.type = queryType
RETURN

I always run into a syntax error, probably cause the syntax is wierd :) Maybe someone has a query where I can set more than one property based on conditions?

Thanks from Balael

Upvotes: 1

Views: 149

Answers (1)

Stefan Armbruster
Stefan Armbruster

Reputation: 39905

Using a CASE WHEN you can build either a map holding the properties you want to set or an empty literal map in case you don't want to set anything. Then use set += {<mymap>} to apply these values:

MERGE (u:User {uuid: {userUUID}}) -[r:relation {rType: {rType}}]-> (n:Node)
ON CREATE SET 
    r.uuid = {relUUID},
    r.status = {relStatus}
ON MATCH 
    SET r += CASE WHEN r.status = "1" 
        THEN {status: {relStatus}, type: {relType}} 
        ELSE {} 
    END
RETURN r;

NB: I have not tried this with parameter but with fixed literal value instead.

Upvotes: 2

Related Questions