Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
04-19-2020 07:01 AM
the current scenario is: There is two-person node A(absd) and B (Ali), now if A Met B, A will form a relationship "MET" e.g A-[: MET{time: "12/12/2019"]->B, now later if B MET A, B has to check is there any relationship exist, if yes, update the properties inside the relationship, else form a MET relationship with the property time with A
I am using this query to check A, B exist, if not form a relationship:
match (p:Person),(a)
where p.id=100 and a.id=2
merge (p)-[:HAS_MET]->(a)
return a, count(*)
Now How B check is there any relationship exists between B and A, if yes then update the property of relationship else create a new relationship.
Solved! Go to Solution.
04-19-2020 01:26 PM
Try this:
match (a:Person) where a.id = 2
match (b:Person) where a.id = 4
optional match (b)-[r:HAS_MET]->(a)
WITH coalesce(r) as r1
//relationship exists..updating relationship property.....
CALL apoc.do.when(r1 is not null, 'match (b)-[r:HAS_MET]->(a) where b.id = 4 and b.id = 2 set r.time = "xxx" RETURN a, b') yield value
return value.a, value.b
//adding relationship.....
CALL apoc.do.when(r1 is null, 'MATCH (a:Person), (b:Person) WHERE a.id = 2 and b.id = 4 merge (b)-[:HAS_MET{time: "vvv"}]->(a) RETURN b, a') yield value
return value.a, value.b
04-19-2020 01:26 PM
Try this:
match (a:Person) where a.id = 2
match (b:Person) where a.id = 4
optional match (b)-[r:HAS_MET]->(a)
WITH coalesce(r) as r1
//relationship exists..updating relationship property.....
CALL apoc.do.when(r1 is not null, 'match (b)-[r:HAS_MET]->(a) where b.id = 4 and b.id = 2 set r.time = "xxx" RETURN a, b') yield value
return value.a, value.b
//adding relationship.....
CALL apoc.do.when(r1 is null, 'MATCH (a:Person), (b:Person) WHERE a.id = 2 and b.id = 4 merge (b)-[:HAS_MET{time: "vvv"}]->(a) RETURN b, a') yield value
return value.a, value.b
04-20-2020 02:08 AM
Hi Ameya,
Finally with your help, i have sorted out the query. It will look like this.
match (a:Person) where a.id = 1
match (b:Person) where b.id = 2
optional match (a)-[a_b:HAS_MET]->(b)
optional match (a)<-[b_a:HAS_MET]-(b)
WITH *, coalesce(a_b) as r1, coalesce(b_a) as r2
//when A and B aleady Met and A->B exist
CALL apoc.do.when(r1 is not null and r2 is null, 'match (a)-[r:HAS_MET]->(b) WHERE a.id = 1 and b.id = 2 set r.time = "a met b" RETURN a, b') yield value
with value as firstSet, r1, r2, a, b
//when A and B already Met and A->B relation does not exist but B -> A exist
CALL apoc.do.when(r2 is not null and r1 is null, 'match (b)-[r:HAS_MET]->(a) WHERE a.id = 1 and b.id = 2 set r.time = "b met a " RETURN a, b') yield value
with value as secondSet, a, b, r1, r2
//When A and B exist, but no relation
CALL apoc.do.when(r1 is null and r2 is null, 'MATCH (a:Person), (b:Person) WHERE a.id = 1 and b.id = 2 merge (a)-[:HAS_MET{time: "firstTime"}]->(b) RETURN a, b') yield value
return a, b
04-19-2020 08:26 PM
You're on the right track with MERGE, now review the documentation on using ON MATCH SET and ON CREATE SET with MERGE, allowing conditional setting of properties in each situation.
All the sessions of the conference are now available online