cancel
Showing results for 
Search instead for 
Did you mean: 

Create New Relations based on Existing Relations for a Node

OmShanti
Node Clone

Warm Greetings to Neo4j Community!

We've following requirement which we are trying to address in Neo4j -

  1. A Node is connected to another Node through many relationships, and each of these relationships capture a number of metrics based on a given date. For e.g

    (A) -[:HAS {metric: 'a1', date: '2021-10-10', value: 40}]->(B)
    (A) -[:HAS {metric: 'a2', date: '2021-10-10', value: 60}]->(B)
    (A) -[:HAS {metric: 'a3', date: '2021-10-10', value: 70}]->(B)
    (A) -[:HAS {metric: 'a4', date: '2021-10-10', value: 10}]->(B)
    
2.  There is a need to Create New relationships between A and B, which will contain exactly the same information as existing relationships except the 'date' property, which is to be updated with a new date.
      
	  (A) -[:HAS {metric: 'a1', date: '2021-10-12', value: 40}]->(B)
	  (A) -[:HAS {metric: 'a2', date: '2021-10-12', value: 60}]->(B)
	  (A) -[:HAS {metric: 'a3', date: '2021-10-12', value: 70}]->(B)
      (A) -[:HAS {metric: 'a4', date: '2021-10-12', value: 10}]->(B)

	  
3.  Requirement is not to Update the existing relations. Only Create new relations with new 'date' but rest of the properties on the relations should be same. 

Considering that there exists 100+ such relationships between A and B for a given date, can we copy(utilize) the existing relations between A and B, and create new relations based on that copied info, with updated 'date' property.

Can you please advice is there an easy way to achieve the objective.

1 REPLY 1

glilienfield
Ninja
Ninja

You can try this. Replace the match(A) and match(B) where predicates with you actual conditions to identify nodes A and B.

match(A) where id(A) = 1
match(B) where id(B) = 2
match (A)-[r_existing:HAS {metric: 'a1', date: '2021-10-12'}]->(B)
merge (A)-[r_new:HAS]->(B)
set r_new = r_existing, r_new.date = $newDate