neo4j - Cypher 'Node Already Exists' 问题与 MERGE

标签 neo4j cypher

当我对位置节点的地址有唯一约束但正在使用合并时,为什么我会遇到此 Cypher 语句的问题,该合并应该会发现它是否存在并且只返回该语句其余部分的 id .我错过了什么?

这是我的声明:

MERGE(l:Location{location_name:"Starbucks", address:"36350 Van Dyke Ave", city: "Sterling Heights",state: "MI", zip_code:"48312",type:"location",room_number:"",long:-83.028889,lat:42.561152})
CREATE(m:Meetup{meet_date:1455984000,access:"Private",status:"Active",type:"project",did_happen:"",topic:"New features for StudyUup",agenda:"This is a brainstorming session to come with with new ideas for the companion website, StudyUup. Using MatchUup as the base, what should be added, removed, or modified? Bring your thinking caps and ideas!"})
WITH m,l 
MATCH (g:Project{title_slug:"studyuup"}) MATCH (p:Person{username:"wkolcz"})
WITH m,l,g,p  
MERGE (g)-[:CREATED {rating:0}]->(m)
MERGE (m)-[:MEETUP_AT {rating:0}]->(l)-[:HOSTED_MEETUP]->(m)
MERGE (m)<-[:ATTENDING]-(p)
RETURN id(m) as meeting_id

我正进入(状态:
Node 416 already exists with label Location and property "address"=[36350 Van Dyke Ave]

最佳答案

您遇到了一个常见的误区MERGE . MERGE合并您在单个 MERGE 中指定的所有内容条款。所以操作顺序是:

  • 搜索 :Location具有您指定的所有属性的节点。
  • 如果找到,则返回节点。
  • 如果未找到,则创建节点。

  • 您的问题出现在第 3 步。因为具有您指定的所有属性的节点不存在,所以它转到第 3 步并尝试创建一个具有所有这些属性的节点。那就是违反了唯一性约束的时候。

    最佳做法是合并您限制为唯一的属性,然后使用 SET更新其他属性。在你的情况下:
    MERGE (l:Location {address:"36350 Van Dyke Ave"})
    SET l.location_name = "Starbucks",
         l.city = "Sterling Heights"
    ...
    

    相同的逻辑将应用于您稍后在查询中合并的关系。如果整个模式不存在,它将尝试创建整个模式。这就是为什么您应该坚持以下最佳实践:
    MERGE (node1:Label1 {unique_property: "value"})
    MERGE (node2:Label2 {unique_property: "value"})
    MERGE (node1)-[:REL]-(node2)
    

    关于neo4j - Cypher 'Node Already Exists' 问题与 MERGE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35381968/

    相关文章:

    azure - 在Azure上安装Neo4j,无法浏览WebAdmin

    graph - 如何在 Neo4J 中导入位于不同路径的数据库?

    neo4j - 领域驱动设计与图数据库

    java - 使用 Java API 在 Neo4J 中延迟加载节点的正确方法

    Neo4j SDN 4 模拟序列对象(不是 UUID)

    Neo4j 密码 : Find exact match to array Node property in WHERE clause

    Neo4j Cypher 查询 : "Unknown identifier" when RETURN DISTINCT

    java - 无法迭代 ExecutionResult

    neo4j - 删除两个节点之间所有相同关系类型但仍保留其中一个

    Neo4j:查询以查找关系最多的节点及其连接的节点