CREATE UNIQUE in neo4j produces duplicate nodes

前端 未结 2 2005
花落未央
花落未央 2021-01-22 14:45

According to the neo4j documentation:

CREATE UNIQUE is in the middle of MATCH and CREATE — it will match what it can, and create what is missing. CREATE

相关标签:
2条回答
  • 2021-01-22 14:52

    Here is how you can do it with CREATE UNIQUE

    MATCH (a:Person {name: 'Alice'}), (b:Person {name:'Bob'}) 
    CREATE UNIQUE (a)-[:knows]->(b), (b)-[:knows]->(a)
    

    You need 2 match clauses otherwise you are always creating the node in the CREATE UNIQUE statement, not matching existing nodes.

    0 讨论(0)
  • 2021-01-22 15:09

    This query should do what you want (if you always want to end up with a single knows relationship between the 2 nodes):

    MATCH (a:Person {name: 'Alice'})
    MERGE (b:Person {name: 'Bob'})
    MERGE (a)-[:knows]->(b)
    RETURN a;
    
    0 讨论(0)
提交回复
热议问题