1、创建一个节点 并给节点分配一个属性
create (n {name: "张三", age: 23})
2、创建节点并且分配属性和标签
create (n:Person {name:"张三"})
3、创建一个关系,给两个节点指定关系类型、方向和绑定一个变量
create ({name: "张三"})-[r:have]->({bookname: "设计模式"})
4、创建两个节点,同时创建关系,给两个节点建立关系指定关系类型、方向和属性
create ({name:"李四"})-[:LOVES{since: "2017"}]->({name: "小花"})
5、给两个已经存在的节点创建一个关系
match (a: Person),(b:Person)
where a.name='张三' and b.name='李四'
create (a)-[r:FRIEND]->(b)
接下来是查询数据
cypher 语句的操作 cypher语句的作用是类似与sql语句的select操作的。在次的话,cypher语句是在match、optional、with、和start语句
里面使用的,用来过滤数据
匹配语句的结构
[MATCH WHERE]
[OPTIONAL MATCH WHERE]
[WITH[ORDER BY][SKIP][LIMIT]]
RETURN [ORDRE BY][SKIP][LIMIT]
1.根据属性来匹配节点
match (n{name:“张三”}) return n
2.根据标签和属性匹配节点信息。
match (n:Person{name:"李四"}) return n
3.任意匹配,返回任意节点的信息
cypher语句允许任意匹配,任意匹配可以匹配与cypher表达式相符合的节点信息返回
3.1 匹配任意节点
match (n) return n
3.2 //该语句是匹配出有任意关系的两个节点
match p = (n) -[r]-> (m) return p
3.3 可选匹配optional
//匹配结果集中如果有丢的部分,则会用null来补充
OPTIONAL MATCH (n)-[r]->(m) RETURN m
4.匹配节点时,指定标签、属性和关系类型
match (n:Person)-[:FRIEND]->(m:Person) where n.name="张三"
5.根据标签过滤 //从图数据库中过滤出来Person标签的数据
match (n) where n:Person return n
6. 根据节点属性过滤
match (n) where n.name = "张三" return n
7. 根据关系属性过滤
match (n)-[k:KNOWS] -> (f) where k.since < 2000 return f
8.字符串匹配
字符串首尾可以使用start with 和end with 去匹配定位字符串,还可以去使用contains去判断是否是包含字符串操作的,
8.1 匹配字符串的开头 //匹配 图中所有的节点,返回节点中包含name属性,且属性的值中以“张”开头的节点
match(n) where n.name starts with '张' return n
8.2 去匹配字符串的末尾 //匹配图中的所有节点,返回节点具有name属性,且属性是以'三'结尾的节点
match (n) where n.name ends with '三' return n
8.3 匹配字符窜的包含匹配
match (n) where n.name contains '三' return n
8.4字符串排除匹配
match (n) where not n.name starts with '张' return n;
9.字符串的正则表达式
//cypher 语句的正则是从java哪里继承过来的,包括支持字符串匹配的标志,包括不区分大小写(?i)、多行(?m) 逗号(?s),正则表达式的解析符号是=~。
9.1 模糊匹配 类似于sql的like
match (n) where n.name =~ '.*小.*' return n //类似于sql里面的'%小%'
9.2 不区分大小写的正则表达式
match (n) where n.name =~ '(i)andr.*' return n
10 where子句里面使用路径匹配
10.1 使用逻辑运算符和路径作为匹配的条件
//在图中匹配名称为"李四"或"王五"与"张三"有任何关系的节点,并返回符合匹配条件的节点信息
match (n {name: "张三"}), (m) where m.name in ['李四', '王五'] and (n)<--(m)
10.2 关系匹配使用not逻辑
//匹配和张三没有关系的人
match (persons),(zhangsan{name : "张三"}) where not (persons) --> (zhangsan) return persons
10.3 关系类型匹配,并使用正则表达式
// 在图中匹配张三节点和所有节点的关系类型是以F开头的所有关系
match (n)-[r]->() where n.name="张三" and type(r)=~ 'F.*' return r
11.使用或逻辑匹配关系
//返回任意节点的关系是KNOWS 或者LOVES的所有节点
match p=() -[:KNOWS | :LOVES] -> (m) return p;
12. 关系深度匹配
12.1 匹配从n到m 任意关系 深度是1到5的节点
match p=(n)-[*1..5]-> (m) return p
12.2 匹配从n到m 任意关系 任意深度的节点信息
match p=(n) -[*]-> (m) return p
13.匹配分页返回
match (n) where n.productname = '苹果' return n skip 10 limit 10
14. 匹配去重返回
match (n) n.productName = '苹果' return distinct n;
15. 匹配分组返回值
match (n) where n.productName = '苹果' return order by n.price desc;
16. 别名返回
match (n) where n.productName = '苹果' return n.productName as pname;
17. WITH语句的使用 with是把第一部分的匹配的结果作为下一部分的输入操作的。
//查询具有10个以上的朋友的并且是姓张的男子,
match (user)-[:FRIENDS]->(friend) wher user.name =~ '张.*' with user,count(friend) as friends where friends > 10 return user;
18. with语句里面使用order by desc 关键字
match (user)-[:FRIEND]->(friend) with user, count(friend) as friends
order by friends desc
skip 1 limit 3 return user;
图数据库NEO4J的cypher语句操作练习
猜你喜欢
转载自blog.csdn.net/tryll/article/details/86505183
今日推荐
周排行