Reputation: 911
I'm trying to do a tricky query. I need to check to see if a relationship exists;
The property can be go negative.
Here's essentially what I'm going for, though this format won't work because you can't have a WITH statement after a MERGE:
MATCH (n1:Type1 {id:{id1}), (n2:Type2 {id: {id2})
MERGE (n1)-[r:RELATIONSHIP]->(n2) WITH CASE
WHEN r.prop + {val} < 0 THEN 0
ELSE r.prop + {val} END as prop
ON CREATE SET r.prop = prop
ON MATCH SET r.prop = prop
This is how it would look it two parts, though I'd like to not have to query twice:
MATCH (n1:Type1 {id:{id1}), (n2:Type2 {id: {id2})
MERGE (n1)-[r:RELATIONSHIP]->(n2)
ON CREATE SET r.prop = 0
MATCH (n1:Type1 {id:{id1})-[r:RELATIONSHIP]->n2:Type2 {id: {id2})
WITH r, CASE
WHEN r.prop + {val} < 0 THEN 0
ELSE r.prop + {val} END as prop
SET r.prop = prop
Upvotes: 2
Views: 3470
Reputation: 911
MATCH (n1:Type1 {id:{id1}), (n2:Type2 {id: {id2})
MERGE (n1)-[r:RELATIONSHIP]->(n2)
ON CREATE SET r.prop =
CASE
WHEN {val} < 0 THEN 0
ELSE {val}
END
ON MATCH SET r.prop =
CASE
WHEN r.prop + {val} < 0
THEN 0
ELSE r.prop + {val}
END
Upvotes: 1
Reputation: 20175
I think this query should do the trick :
MATCH (n1:Type1 {id:{id1}), (n2:Type2 {id: {id2})
MERGE (n1)-[r:RELATIONSHIP]->(n2)
ON CREATE SET r.prop = 0
ON MATCH SET r.prop =
CASE r.prop
WHEN r.prop + {val} < 0
THEN 0
ELSE r.prop + {val}
END
Upvotes: 1