Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Neo4j relationship: update a property based on its old value

When creating two relationships with the exact same properties, I want Neo4j to update a property of the relationship based on the previous value of that property. For instance, increment a counter.

Any thoughts on how I can update the following query to achieve this?

CREATE (p:Person {name: "Tom Hanks"})
CREATE (m:Movie {title:"You've Got Mail"});
MATCH (p:Person {name: "Tom Hanks"})
MATCH (m:Movie {title:"You've Got Mail"})
CALL apoc.merge.relationship(p, "ACTED_IN",
  {roles:['Joe Fox']},
  {created: datetime(),  counter: 0},
  m,
  {lastSeen: datetime(), counter: 1}  // Update this to something like `counter++`
)
YIELD rel
RETURN rel;

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I’ve switched to the declarative form of Cypher:

MATCH (p:Person {name: "Tom Hanks"})
MATCH (m:Movie {title:"You've Got Mail"})
MERGE (p)-[rel:ACTED_IN {roles:['Joe Fox']}]->(m)
ON CREATE SET rel.created = datetime(), rel.counter=0
ON MATCH SET rel.lastSeen = datetime(), rel.counter=rel.counter+1
RETURN rel;

When the relationship is created, the counter is set to 0; otherwise it is updated.

Note: I took the declarative counterpart of the query from Neo4j Docs, and I added the part you’re interested to.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading