最近一直在做图数据库的相关工作,对neo4j的查询语言Cypher使用较多,故在此总结记录。Cypher作为图数据库的查询语言,感觉和关系型数据库的查询语言sql差不多吧。
1.如何找到一个节点x,x以某种关系同时连接两个不同节点a和b
match (a)-[r:relation]->(x)<-[r:relation]-(b) return x
2.如何找到节点a和b之间的最短路径
(1)match p=shortestpath((a)-[r:relation]-(b)) return nodes(p)
(2)match(n:na{name:’###’}),(m:nb{name:’###’})with n,m match p=shortestpath((n)-[r*…]-(m)) return p;
3.如何找到节点a和b之间以某种关系相连接的最短路径
p=shortestpath((a)-[r:relationname]->(b)) return nodes(p)
4.找到数据库中出现的唯一节点标签
match n return distinct labels(n)
5.找到数据库中出现的唯一关系类型
match n-[r]-() return distinct type(r)
6.找到数据库中的唯一节点标签和唯一关系类型
match n-[r]-() return distinct labels(n),type(r)
7.找到不与任何关系(或某种关系)向连的节点
start n = node() match n-[r:relationname]-() where r is null return n
8.找到某个带有特定属性的节点
start n=node() match n where has (n.someproperty) return n
9.找到与某种关系相连接的全部节点
start n= node() match n-[r:relati