user2103008
user2103008

Reputation: 524

neo4j update array property if exists

I'm using neo4j 2.0 for my project. I want to add a relationship using Cypher if it doesn't exist else update an array property.

MATCH (a:Term), (b:Term) 
WHERE a.Name = 'abc' AND b.Name = 'xyz' 
CREATE UNIQUE a-[r:gives]->b
SET r.positive = coalesce(r.positive + (last(r.positive)/2),[0.125])
RETURN r;

However, using coalesce and last, I get an error (basically r.positive doesn't exist when a new relationship is created, maybe I'm not using coalesce right)

Don't know how to Divide(LastFunction(Product(r,positive(14),true)),Literal(2)) `2` with `null`

Is there another way to write this query ?

Thanks

Upvotes: 2

Views: 2294

Answers (1)

jjaderberg
jjaderberg

Reputation: 9952

The coalesce is fine, but when r.positive is null, last(r.positive) is also null, and you can't divide null in half.

Does this do what you expect?

MATCH (a:Term),(b:Term) 
WHERE a.name = 'abc' AND b.name = 'xyz' 
CREATE UNIQUE (a)-[r:gives]->(b) 
SET r.positive = 
  CASE WHEN NOT (HAS (r.positive)) 
    THEN [0.125]
  ELSE r.positive + LAST(r.positive)/2 END 
RETURN r

Upvotes: 3

Related Questions