4

I need to create a relation between two users once, and update its properties since then. Is there a way to do something like "create if not exist otherwise update" in Neo4j using Cypher?

HanXu
  • 5,507
  • 6
  • 52
  • 76

1 Answers1

7
MERGE (u1:User)-[r:REL]->(u2:User)
ON CREATE SET
    u1.prop1 = val1,
    u2.prop2 = val2,
    r.prop3 = val3
ON MATCH SET
    u1.prop1 = newVal1,
    u2.prop2 = newVal2,
    r.prop3 = newVal3

Have a look at the Neo4j docs for "MERGE".

BtySgtMajor
  • 1,380
  • 10
  • 26
  • Oops, I looked but missed this!..thanks! BTW, could you have a look at my another question? I am new to neo4j and not sure am doing things right. http://stackoverflow.com/questions/23831483/ways-to-remember-and-reuse-previous-query-result-in-neo4j – HanXu May 23 '14 at 14:38
  • As much as I'd like to, unfortunately I'm not familiar with Neography. I'm sure someone in our fine community can help, though. And, if this answer solves your issue, please be sure to mark the answer as "accepted". Thanks and good luck! – BtySgtMajor May 23 '14 at 14:40
  • 1
    I found the query incorrect,there should be ON MATCH clause instead of ON MERGE, may be the neo4j documentation has been updated. Please update the query, http://neo4j.com/docs/developer-manual/current/cypher/clauses/merge/ – Prabjot Singh Apr 25 '17 at 10:15
  • For something branded as "intuitive" I've spent far too many hours chasing this down. Thank you! – pojo-guy Jan 21 '19 at 00:57