Cypher语言是在学习Neo4j时用到数据库操作语言(DML),涵盖对图数据的增删改查
neo4j数据库简单除暴理解的概念:
Neo4j中不存在表的概念,只有两类:节点(Node)和关联(Relation),可以简单理解为图里面的点和边。
在数据查询中,节点一般用小括号(),关联用中括号[]。
当然也隐含路径的概念,是用节点和关联表示的,如:(a)-[r]->(b),表示一条从节点a经关联r到节点b的路径。
1、Match
查找全部节点和关系(2种查询)
match(n) return n
match(n)-->(m) return n,m
MATCH path = () -- () RETURN path
查找节点 ( )
match(a:person) where a.name='Keanu Reeves' return a
查找关系 [ ]
以下关系都是合法的:
( ) -- ( ) ( ) -> ( ) ( ) <-- ( )
( ) - [ ] - ( ) ( ) - [ ] -> ( ) ( ) <- [ ] - ( )
(4种查询):找到”Keanu Reeves“出演的所有电影
match(a:person)-[r:ACTED_IN]-(b:movie) where a.name='Keanu Reeves' return a,b,r
//a 和 b 是变量,保存节点 r 是变量,保存关系
match(a:person{name:'Keanu Reeves'})-[:ACTED_IN]->(b:movie) return a,b
//a 和 b 是变量,保存节点
match(a:person{name:'Keanu Reeves'})-->(b) return a,b
//a 和 b 是变量,保存节点
match path = (:Character{name:"Aggo"})--(:Character) return path
//path 是变量,保存路径
查找没有关系的节点
match (n) where not (n)–-() return n
2、Where
match(a:person) where a.name<>'Keanu Reeves' return a
match(a:person)-[r:ACTED_IN]-(b:movie) where a.name<>'Keanu Reeves' return a,b,r
3、create
create(hugo:person{name:'Hugo Weaving'}) return hugo