Neo4j
一、创建节点
create (Kungfu:Move {title:'Kungfo', released:2008, tagline:'made in china'})
二、创建人物节点
create (Zhouxingchi:Person {name:'zhouixingchi', born:1980})
三、创建演员导演关系
create (Zhouxingchi)-[:acted_in {role:'self'}]->(Kungfo)
,
create (Zhouxingchi)-[:directed]->(Kungfo)
四、查找节点
1.按名字查找:
match (xingye {name:'zhouxingchi'}) return xingye
2.查找电影
match (kf {title:'Kungfo'}) return kf
3.随机查找多个人物
match (people:Person) return people.name limit 10
4.查找多个电影
match (mv:Movie) where mv.released >1970 and mv.released < 2000 return mv.title
五、查找关系
1.查找演员参演过的电影
match (xingye:Person {name:'zhouxingchi'})-[:acted_in]->(xingMovie) return xingye, xingMovie
2.查看谁导演了电影
match (dianying {title:'Kungfo'})<-[:directed]-(daoyan) return daoyan.name
3.查看与zhouxingchi同出演过电影的人
match (xingye:Person {name:'zhouxingchi'})-[:acted_in]->[m]<-[:acted_in]-(yanyuan) return yanyuan.name
4.查找与kungfo相关的所有人(人名、电影关系类型、电影的关系)
match (people:Person)-[relatedTo]-(:Movie {title:'kungfo'}) return people.name, Type(relatedTo), relatedTo
六、查询关系路径
1.查找与演员zhouxingchi存在4条及以内的任何演员及电影
match (bacon:Person {name:'zhouxingchi'})-[*1..4]-(movies) return distinct movies
2.查找演员之间最短路线
match p=shortestPath( (bacon:Person {name:'zhouxingchi'})-[*]-(meg:Person {name:'zhuyin'}) ) return p
3.查找没有与zhouxingchi合作过的演员
match (xingye:Person {name:'zhouxingchi'})-[:acted_in]->(m)<-[:acted_in]-(yanyuan), (yanyuan)-[:acted_in]->(m2)<-[acted_in]-(yanyuan2) where not xingye-[:acted_in]->(m2) return yanyuan2.name as unxingstar, count (*) as strength order by strength.desc
4.找人将zhouxingchi介绍给zhuyin
match (xingye:Person {name:'zhouxingchi'})-[:acted_in]->(m)<-[:acted_in]-(yanyuan), (yanyuan)-[:acted_in]->(m2)<-[acted_in]-(zixia:Person {name:'zhuyin'}) return xingye, m, yanyuan, zixia
七、清空数据库
1.清空所有Person、Movie节点
match (a:Person), (m:Movie) optional match (a)-[r1]-(), (m)-[r2]-() delete a, r1, m, r2
2.检查任意数据
match (n) return n