Balael
Balael

Reputation: 213

Change a property with condition

The query shall check wether a relation is already existing and in that case should check if a property has a special value and change it depending on the check. If that relation doesn exist it shall be created.

I have tried several ways and what comes closest is

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 = {relStatus}
    WHERE r.status = "1"   // Only if r.status of the existing pattern is 1 it shall be changed to the value of relStatus
RETURN r

The WHERE isnt correct syntax - maybe someone has a hint how I can check a property and only change the property based on a special trigger when using ON MATCH.

Thanks.

Upvotes: 0

Views: 702

Answers (1)

cybersam
cybersam

Reputation: 66957

This should work:

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;

Upvotes: 2

Related Questions