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 將合併失敗

相關文章
相關標籤/搜索