Cypher - MERGE

 能够确保图数据库中存在某个特定的模式。若是该模式不存在,那就建立它。
 能够指定让某个数据存在,无论是匹配到仍是建立
 在整个模式上使用,要么整个匹配,要么整个建立,不能部分匹配数据库

//匹配某个节点,若是不存在就建立,存在就返回spa

merge (u:User{id:8,name:'吴用'})
return u
date

//根据条件来匹配  , 若是 u.id 重复出现,也只会建立一次
match (u:User)
merge (a:Auth{userId:u.id})
return a
im

// 检查节点是否存在,若是不存在则建立它并设置属性数据

merge (u:User{id:9,name:'林冲'})
on create set u.createTime = timestamp()
return u
co

// 匹配节点,并在找到的节点上设置属性block

merge (u:User{id:3})
on match set u.name='武松' 
return u
time

// create 与 match 同时使用return

merge (u:User{id:4})
on create set u.name = '晃盖' , u.createTime = timestamp()
on match set u.name = '史进' , u.updateTime = timestamp()
return u

//匹配或建立关系。 注意必须至少指定 个绑定的节点

match (u1:User),(u2:User)
where u1.id = 6 and u2.id = 9
merge (u1)-[:DIRECT]->(u2)
return u1,u2

//若是 (o) 不存在,就建立节点。并建立它们的关系

match (u1:User),(u2:User)
where u1.id = 1 and u2.id = 11
merge (u1)-[:DIRECT]->(o)->(u2)
return u1,u2

//合并已存在两节点以前的关系

match (u:User{id:1})
merge (a:Auth{userId:u.id})
merge (u)-[:UA]->(a)

return u,a

//合并一个已存在节点和一个合并的节点之间的关系

match (u:User{id:1})
merge (u)-[:UA]->(a:Auth{userId:u.id})
return u,a

merge 的惟一性约束
 -- 当使用的模式涉及惟一性约束时,cypher 能够经过 merge 来防止获取相冲突的结果

1. 惟一性约束与部分匹配      -- merge 将合并失败

2. 惟一性约束与匹配冲突      --merge 将合并失败

相关文章
相关标签/搜索