neo4j命令
neo4j命令
初始化执行
为所有shop标签节点创建唯一uid约束
创建约束
CREATE CONSTRAINT ON (e:shop) ASSERT e.uid IS UNIQUE
创建节点
create(n:代理{name:"",uid:"",level:1})
return n
create (x:MALE{city_beento:['北京', '深圳']}) 带数组属性的节点
create constraint on (n:MALE) assert n.name is unique 创建带限定的节点
drop constraint on (n:MALE) assert n.name is unique 删除限定条件
查询
MATCH (n:MALE) return n; 多个标签查询
MATCH (n:MALE:TEACHER) return n;
match (a:MALE{ name:"John", age:24} return a 标签+属性查询
match (n)
return ID(n) 返回自带的id
带条件查询
match (x)
where x.age < 30 and x.country = "US" 大于小于等于
return x
match (x)
where x.name in ["John", "Andrew"] and x.age is not NULL 带in和not null
return x
match (x)
where x.name =~ "J.*" 带正则
return x
match (x)
where exists(x.age) 存在
return x.name
match (x)
where x.name starts with "B" 字符串包含
return x.name
match (x)
where x.name contains "a"
return x.name
match (x)
return x
order by x.age skip 3 limit 2 排序和分页
match (x{name:"John"}),(y{name:'Annie'}) 返回从句
return x,y
union和union all
match (x:MALE)-[:FRIEND]->() return x.name, labels(x)
union
match (x:FEMALE)-[:FRIEND]->() return x.name, labels(x)
MATCH (x{name:""})--(y)--() with x create(n:代理{name:"",age:23})-[r:children]->(x) RETURN n,r,x
使用别名
with 'AGE' as haha
match (x)
where x[toLower(haha)] < 30
return x
查询并创建关系
MATCH (e:李四),(c:张三) CREATE (e)-[r:CHILDREN]->(c)
match (a:TEACHER)-[b:TEACHES]->(c:STUDENT) return a,b,c
创建关系
create (n:MALE{name:'Kevin'})-[r:FATHER]->(n1:MALE{name:'Leo'}) return n,r,n1
create (n:MALE{name:'行家店主1'})-[r:sub]->(n1:MALE{name:'行家店主2'}), (n1)-[r1:sub]->(n2:MALE{name:'行家店主3'}) return n,r,n1
创建多点并建立关系
create (n1)-[r1:friend]->(n2)-[r2:friend]->(n3),(n2)-[r3:friend]->(n4) return n1,r1,n2,r2,n3,r3,n4
创建节点并建立关系
create(n:代理{name:"行家店主1",uid:"kh1223423",level:1}) <-[r:sub]-(n1:MALE{name:"Leo"}) return n,r,n1
关系中带属性
create k = (n1)-[r1:friend{location:'China'}]->(n2) return k
创建并建立关系
create (f1:FEMALE{name:"Sheena"}),(m:MALE{name:"Oliver"}),(f2:FEMALE{name:"Sally"}),(f1)-[r:FRIEND]->(m)-[r1:FRIEND]->(f2)
return f1,r,m,r1,f2
更改属性
match(n:shop{uid:'xx'}) set n.level=1 return n
match (n:FEMALE {name:"Sheena"}) set n.surname = "Foster" return f
更改标签
match (n{name:'Sheena'})
set n:GOODMAN:BADMAN
return n
更改联系
match (n{name:'Sheena'})-[r:FRIEND]-(n1{name:"Oliver"})
delete r
return n,n1
统计
match(n:代理) where n.name="" return count(n)
match(n:代理) where n.name="" return count(distinct n.uid) 分组统计
移除节点某属性
match (n:代理{name:""}) remove n.uid return n
MATCH (e:shop{uid:'xx'})
MERGE (e1:shop:level{uid:'xx'})
MERGE (e)-[r:sub]->(e1)
return e1.uid as uid
删除
match (n{uid:'xx'})-[r]-(n1)
delete n,r,n1;
match (n:shop{uid:'xx'})<-[r:sub]-(n1)
delete n,r
MATCH (a:{uid:'kh127'})-[r]->(b)-[r]->(c) where b.id = 48 DETACH delete a,r,c
match (n)
delete n;
MATCH (e:shop{uid:'xx'}) return e
MATCH (cc)-[rel]->(c:fans)
DELETE c,rel
MATCH (cc:shop{uid:"kh128"})-[sub]->(c:shop{uid:"kh129"})
DELETE sub
MATCH (cc:shop{uid:"xx"})-[r]-(c:shop)
match (n{name:'Sheena'}) set n.middlename = NULL return n 删除属性
match (n{name:'Sheena'}) remove n.surname return n 删除属性
全部删除
match(n)
detach delete n
match (n)-[r]-(n1)
delete n,r,n1;
match (n)
delete n;
WITH 语句
在Cypher中,可以将语句片段链接在一起,就像在数据流管道中一样。每个片段都处理前一个片段的输出,其结果可以输入到下一个片段。
您可以使用该WITH子句组合各个部分,并声明哪些数据从一个流到另一个。 WITH非常RETURN不同的是,它没有完成查询,但为下一部分准备输入。您可以使用与RETURN子句中相同的表达式,聚合,排序和分页。
唯一的区别是您必须为所有列添加别名,否则它们将无法访问。只有您在WITH子句中声明的列才可用于后续查询部分。
请参阅下面的示例,其中我们收集某人出现的电影,然后过滤掉仅出现在一部电影中的电影。
MATCH (person:Person)-[:ACTED_IN]->(m:Movie)
WITH person, count(*) AS appearances, collect(m.title) AS movies
WHERE appearances > 1
RETURN person.name, appearances, movies
不知道节点是否存在的情况下,合并节点,并创建关系
MERGE (e:shop:level{name:"行家店主1",uid:"kh128"})
MERGE (e1:shop:level{name:"行家店主1",uid:"kh129"})
MERGE (e)-[r:sub]->(e1)
return e,r,e1
创建约束
CREATE CONSTRAINT ON (e:xx) ASSERT e.ss IS UNIQUE
CREATE CONSTRAINT ON (e:fans) ASSERT e.uid IS UNIQUE
删除约束
DROP CONSTRAINT ON (e:level3) ASSERT e.uid IS UNIQUE
查询指定节点可变长路径里子路径整个path (0起是包括自己)
MATCH (level3{uid:'kh123'})-[:sub*0..2]-(m:level3)
RETURN m
查询指定节点可变长路径里子路径整个path(可指定下级标签,不写就是所有下级)
MATCH (n{uid:'kh123'})-[*0..2]->(m)
RETURN n,m
查询指定节点可变长路径里父路径整个path
MATCH (n{uid:'kh127'})<-[:sub*0..2]-(m)
RETURN m,n
查询指定节点可变长路径里子路径整个path节点数量(包括自己)
MATCH (n{uid:'kh127'})-[*0..2]-(m)
RETURN count(m)
MATCH (m:level3)-[:sub*0..2]-(level3{uid:'kh127'})
RETURN m
多个查询连接
MATCH (cc:CreditCard) RETURN cc.id,cc.number
UNION
MATCH (dc:DebitCard) RETURN dc.id,dc.number
MATCH (m:level3{name:"行家店主1"})<-[:sub]-(a:level3{name:"行家店主1",uid:"kh123"})
RETURN m.name AS name,m.uid as subuid,collect(a.uid) AS uid, count(*) AS actors
查询已创建的约束
:schema
执行计划
profile xx
MATCH (n:shop{uid:'xx'})-[r]-(m)
RETURN n,r,m