Difference between merge and create unique in Neo4j

前端 未结 1 860
谎友^
谎友^ 2021-02-02 09:00

I\'m trying to figure out what is the difference between MERGE and CREATE UNIQUE. I know these features:

MERGE

I\'m able to create node, if doesn\'t exist patt

相关标签:
1条回答
  • 2021-02-02 09:31

    CREATE UNIQUE has slightly more obscure semantics than MERGE. MERGE was developed as an alternative with more intuitive behavior than CREATE UNIQUE; if in doubt, MERGE is usually the right choice.

    The easiest way to think of MERGE is as a MATCH-or-create. That is, if something in the database would MATCH the pattern you are using in MERGE, then MERGE will just return that pattern. If nothing matches, the MERGE will create all missing elements in the pattern, where a missing element means any unbound identifier.

    Given

    MATCH (a {uid:123})
    MERGE (a)-[r:LIKES]->(b)-[:LIKES]->(c)
    

    "a" is a bound identifier from the perspective of the MERGE. This means cypher somehow already knows which node it represents.

    This statement can have two outcomes. Either the whole pattern already exists, and nothing will be created, or parts of the pattern are missing, and a whole new set of relationships and nodes matching the pattern will be created.

    Examples

    // Before merge:
    (a)-[:LIKES]->()-[:LIKES]->()
    
    // After merge:
    (a)-[:LIKES]->()-[:LIKES]->()
    
    
    // Before merge:
    (a)-[:LIKES]->()-[:OWNS]->()
    
    // After merge:
    (a)-[:LIKES]->()-[:OWNS]->()
    (a)-[:LIKES]->()-[:LIKES]->()
    
    
    // Before merge:
    (a)
    
    // After merge:
    (a)-[:LIKES]->()-[:LIKES]->()
    
    0 讨论(0)
提交回复
热议问题