Neo4j常用的查詢


參考:https://www.cnblogs.com/mzyn22/p/11098285.html

一、添加操作

  1. 添加節點:

    create (x:學生{studentId:'1001',age:20}

  2. 添加關系:

    對現有的節點添加關系

      match (x:學生{studentId:1001}),(y:教師{tid:'09'}) create (x)-[jx:課程{name:'高數'}]->(y)

      match (x:學生),(y:教師) where x.studentId='1001' and y.tid='09' create (x)-[jx:課程{name:'高數'}]->(y)

    添加節點並添加關系

      create (x:學生{studentId:1001})-[jx:課程{name:'高數'}]->(y:教師{tid:'09'})

  3. 根據節點的ID創建關系

      match (x:學生),(y:教師) where id(x)=254885 and id(y)=554896 create (x)-[jx:課程{name:'高數'}]->(y)

二、刪除操作

  1. 刪除節點:

    match (x:學生{studentId:1001}) delete x

  2. 刪除關系:

    match (x:學生{studentId:1001})-[jx:課程{name:'高數'}]->(y:教師{tid:'09'})  delete jx

  3. 刪除關系的同時,刪除數據:

    match (x:學生{studentId:1001})-[jx:課程{name:'高數'}]->(y:教師{tid:'09'})  delete x,jx,y

三、修改節點

  1. 給節點添加一個新的屬性,兩種方式:

    match(x:學生{studentId:'1001'}) set x.age=21 return x

    match(x:學生{studentId:'1001'}) set x+={age:21} return x

  2. 給節點添加屬性並刪除現有屬性

    match(x:學生{studentId:'1001'}) set x={age:21,name:'abc'} //注意這里,會將studentId屬性刪除

  3. 添加新標簽:

    match(x:學生{studentId:'1001'}) set x:男生 return x  //添加一個標簽

    match(x:學生{studentId:'1001'}) set x:男生:團員 return x  //添加多個標簽

四、查詢操作

  1. 根據節點屬性查找對應節點:

    match(x:Student{studentId:'1001'}) return x

    或者

    match(x:Student) where x.studentId='1001' return x

  2. 根據關系查找節點

    match (x)-[r:教學內容]-(y) where r.課程='語文' return x,r,y

  3. 查詢單獨的節點,即:與其他任何節點沒有任何關系

    match(x) where not (x)-[]-() return x

  4. 查詢N層關系的節點:

    match q=(x)-[*5..8]-() return q limit 200 這個為查詢5到8層關系的

    match q=(dh)-[r]-(jq)-[rr]-()-[]-()-[]-()-[]-()-[]-()-[]-() return q limit 400

  5. 查詢節點關系數個數:

    match(dh:`學生`)-[r]-(jq:`老師`) with dh, count(r) as dhs where dhs > 2 return dh

  6. 查詢節點個數:

    match(x) return count(x)

  7. 查詢所有的關系類型:

    CALL db.relationshipTypes()

  8. 查詢所有的節點標簽:

    CALL db.labels()

   9. 查詢節點關系種類:

    CALL db.schema()

  10. 最短路徑查詢

    MATCH (x:電話{hm:"02711111111"}),(y:電話{sjdbh:"025111111111"}),p=shortestpath((x)-[*..10]-(y))RETURN p

  11. 查詢不存在某個屬性的節點

    match(x:電話) where x.repeat is null with x match p=(x)-[r*1..5]-(y) return p

  12. 查詢存在關聯到某一個節點具有相同屬性的其他的節點

    match p=(x)-[]-()-[]-(y) where x.name=y.name return p

  13. 查詢兩個節點之間存在多個關系的節點

    match p=(x)-[r1]-(y)-[r2]-(x) where id(r1)<>id(r2) return p

  14. 查詢某一個節點具有m到n層關系的所有的節點

    match q=(x:學生)-[*1..5]-() where x.no ='201921011XXXX' return q  #查詢某學生有關系的節點,最多五層關系


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM