Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update existing relationship in neo4j?

Tags:

neo4j

cypher

I have two nodes A and B. They have a relationship R, with some property P, on this relationship.

How I can update this relationship R, with a new value for P? I tried merge, but this creates a new relationship, but I want to update the existing one.

like image 422
Old Programmer Avatar asked Dec 25 '16 06:12

Old Programmer


People also ask

How do I change my relationship name in Neo4j?

You cannot rename an already existing relationship. You'd have to run through all relationships, create the new one in parallel (including all properties) and then remove the old one.

How do you create a relationship between existing nodes in Neo4j?

To create a relationship between two nodes, we first get the two nodes. Once the nodes are loaded, we simply create a relationship between them. The created relationship is returned by the query.

How are relationships stored in Neo4j?

Properties are stored as a linked list of property records, each holding a key and value and pointing to the next property. Each node and relationship references its first property record. The Nodes also reference the first relationship in its relationship chain. Each Relationship references its start and end node.

What is unwind in Neo4j?

With UNWIND , you can transform any list back into individual rows.


1 Answers

Match on your nodes and the relationship, then use SET to update the relationship property. For example:

MATCH (a {name:"A"})-[r]-(b {name:"B"})
SET r.P = "bar"

It's generally best, when looking up specific nodes, to use labels in the query, and have an index or unique constraint (whichever makes the most sense) to speed up your lookups.

like image 59
InverseFalcon Avatar answered Oct 21 '22 15:10

InverseFalcon