欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

操作图数据库的语言--Cypher

程序员文章站 2022-06-12 10:06:05
...

本章节主要说下Cypher的基本操作:
官方文档:https://neo4j.com/docs/cypher-manual/3.5/
Cypher是一种用来处理图数据库的语言,语法比较简单。

查询
查询所有点

MATCH (n) RETURN n

查询某一Label的点

MATCH (n: Orders) RETURN n

根据UserId和Label查询唯一点

MATCH (n: Orders)  WHERE uid(n) = "12345"  RETURN n

根据Id查询边

MATCH()-[r]-()  WHERE id(r) = "11111_22222"  RETURN r

查找某一个点的邻居

MATCH (a: Orders)-[r]-(b) WHERE uid(a) = "12345"  RETURN r, b

查找某一个点的邻居 (指定关系Label和邻居Label)

MATCH (a: Orders)-[r: SoldBy]-(b: Employee)  WHERE uid(a) = "12345" RETURN r, b

查找两个点之间的路径

MATCH p = (from: Customer)-[]->(to: Orders)  WHERE uid(from) = "11111" AND uid(to) = "22222" RETURN p

查找两个点之间的路径(指定路径长度范围)

MATCH p = (from: Customer)-[*1..3]->(to: Orders)WHERE uid(from) = "11111" AND uid(to) = "22222"  RETURN p

查找两个点之间的最短路径

MATCH p = shortestPath((from: Customer)-[*..5]->(to: Orders))

按业务Id查询点

MATCH n WHERE n.__id = "12345" RETURN n

按名称模糊查询点

MATCH n  WHERE n.__name =~ "(?i).*Maria.*" RETURN n

创建结点
创建点 (指定UserId)

CREATE (n: Orders {__uid: "12345"})

创建点 (指定UserId, 属性)

CREATE (n: Orders {__uid: "12345", total: 100})

更新点

MATCH (n: Orders) WHERE uid(n) = "12345" SET n.total = 100

删除点

MATCH (n: Orders)  WHERE uid(n) = "12345" DELETE n

创建边(起点与终点也一起创建)

CREATE (src: Orders)-[r: SoldBy]->(dst: Employee)

创建边 (在已有点上创建)

MATCH (src: Orders), (dst: Employee) WHERE uid(src) = "11111" AND uid(dst) = "22222" CREATE (src)-[r: SoldBy]->(dst) 

更新边

MATCH ()-[r]-()  WHERE id(r) = "11111_22222" SET r.property = 1

删除边

MATCH ()-[r]-() WHERE id(r) = "11111_22222" DELETE r

创建俩个结点并建立关系

 create(n:Orders{uid:"12356",Employeename:Tom}),  
 (m:Orders{uid:"12357",Employeename:Joe}),
 (n)-[: KNOWS]->(m)

查询俩个节点以及关系

MATCH(n:Orders)-[KNOWS]-(m:Orders) returen n,KNOWS,m

删除俩个结点以及它们的关系

MATCH (n:Orders)-[KNOWS]-(m:Orders) DELETE n,KNOWS,m
相关标签: cypher