2
votes

I'm Neo4j noob and I'm trying to create unique relationship between two nodes depending on relationship properties.

Let's say we have node A and node B. I would like to create a new relationship R between A and B, if R.since = 1 or R.since IS NULL. Otherwise I want to get an existing relationship.

I tried to do it like this:

MATCH (n:Crew { name: "Neo" }),(m:Matrix { name: "Agent Smith" })
MERGE (n)-[r:CATCH]->(m)
ON CREATE SET r.since = 1
WITH r WHERE r.since IS NULL OR r.since = 1 AND r.source = "ab"
RETURN r

but query returns nothing.

1
You're probably missing the source property on the relationship. If you remove the AND clause, you should get a result.tstorms

1 Answers

0
votes

Better to try with this code:

MATCH (n:Crew),(m:Matrix) WHERE n.name="Neo" and m.name="Agent Smith"
MERGE (n)-[r:CATCH]->(m)
ON CREATE SET r.since = 1
WITH r WHERE r.since IS NULL OR r.since = 1 AND r.source = "ab"
RETURN r