一:查询
比较操作:
=
<>
<
>
<=
>=
布尔操作:
AND
OR
NOT
XOR
1、把节点的前两个字为"提示"的节点去除"提示":
match(l) where l.name=~'提示.*'
with collect(l.name)
as result
unwind result as row
return substring(row,2)
2、把带提示的节点,更新为不带提示:
match(l) where l.name=~'提示.*'
with collect(l.name)
as result
unwind result as row
match(h) where h.name=row set h.name=substring(row,2)
return h
3、分组查询,每个标签的数目,按名字的数目倒排
match(l)
with collect(l.name) as collectName
unwind collectName as p
return p,count(*)as num order by num desc
4.查询不存在emergency属性的疾病
match(d:Disease) where not exists (d.emergency) return d.name
5.查询Condition标签中包含"任二"的节点
match(c:Condition) where c.name contains "任二" return c.name
6.查询疾病没有high_risk属性的节点
match(d:Disease) where d.high_risk is NULL return d.name
7.更新标签名
MATCH (n:User) REMOVE n:User set n:Person RETURN n
8.更新关系名
MATCH (n:User {name:"foo"})-[r:REL]->(m:User {name:"bar"})
CREATE (n)-[r2:NEWREL]->(m)
SET r2 = r
WITH r
DELETE r
9.其他
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:relationshipname]-() return distinct n
10.找到节点和它们的关系数,并以关系数目降序排列显示
start n=node() match n-[r]-() return n,count(r) as rel_count order by rel_count desc
11.返回图中所有节点的个数
start n = node() match n return count(n)
12.(1)删除图中关系:start n=node(*) match n-[r]-() delete r
(2)删除图中节点:start n =node(*) match n delete n
(3)删除图中所有东西:match (n) detach delete n
13.查询某类节点下某属性为特定值的节点
match (n:person)where n.name=”alice” return n
14.with
Cypher中的With关键字可以将前步查询的结果作为后一步查询的条件,这个在我的工作中可是帮了大忙哈哈。下面是两个栗子。
(1)match(p:node_se)-[re:推理条件]->(q:node_se) where p.name=‘FEV1%pred’and p.value=’<30%’ WITH p,re,q match (q:node_se) <-[re2:推理条件]- (c:node_se)return p, re,q,re2,c
(2)match(p:node_patient)-[re:个人情况]->(q:node_se) where p.name=‘qwe’ WITH p,re,q match (q:node_se) -[re2:推荐方案]-> (c:node_se) where q.name=‘first’ WITH p, re,q,re2,c match (c:node_se)-[re3:方案细节]->(d:drugs) return p, re,q,re2,c,re3,d
15.查询符合条件的某个节点的id
match(p) where p.name = ‘***’ and p.value = ‘***’ return id(p)
16.直接连接关系节点进行多层查询
match(na:bank{id:‘001’})-[re1]->(nb:company)-[re2]->(nc:people) return na,re1,nb,re2,nc
17.可以将查询结果赋给变量,然后返回
match data=(na:bank{id:‘001’})-[re1]->(nb:company)-[re2]->(nc:company) return data
18.变长路径检索
变长路径的表示方式是:[*N…M],N和M表示路径长度的最小值和最大值。
(a)-[ *2]->(b):表示路径长度为2,起始节点是a,终止节点是b;
(a)-[ *3…5]->(b):表示路径长度的最小值是3,最大值是5,起始节点是a,终止节点是b;
(a)-[ *…5]->(b):表示路径长度的最大值是5,起始节点是a,终止节点是b;
(a)-[ *3…]->(b):表示路径长度的最小值是3,起始节点是a,终止节点是b;
(a)-[ *]->(b):表示不限制路径长度,起始节点是a,终止节点是b;
19.Cypher对查询的结果进行去重
栗:match(p:node_se)-[re]->(q)where re.name <> ‘and’ return distinct(re.name)
(注:栗子中的<>为Cypher中的操作符之一,表示‘不等于’)
20.更新节点的 labels
Neo4j中的一个节点可以有多个 label,返回所有节点的label:match (n) return labels(n)
修改节点的 label,可以先新加 label,再删除旧的label
match (n:label_old) set n:label_new remove n:label_old
match(n:label_new) return labels(n)
21.更新节点的属性
match(n:) set n.new_property = n.old_property remove n.old_proerty
22.根据关系模糊查询
match(d:医保手术和操作名称)-[r]->(l) where type(r)=~'医保手术和操作名称禁忌.*' return d.name,type(r),collect(l.name)
23.如果找到就设置属性,没找到创建节点并设置属性
merge on create on match:
merge (keanu:Person {name:"Keanu"}) on create set keanu.created=timestamp() on match set keanu.lastSeen=timestamp() return keanu;
24.merge 上使用唯一性约束
CREATE CONSTRAINT ON (n:Person) ASSERT n.name IS UNIQUE;
25。去除开头的空格
match (n) where n.name=~' .*' set n.name=substring(n.name,1) return n.name,size(n.name)