Advertisements
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;
>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.