删除节点属性:
match(n:测试123) where n.code='123'REMOVE n.name
新增label
create(n:fgffdsd{name:'dddd'})
查询某个节点
match(n:测试123) where n.code='123'return n
查询某个label所有节点
match(n:测试123)return n
查询某个节点所有的关系节点
match p=(n:测试123)-[]-()return p
查询所有的节点直接的关系
match p=()-[]-()return p
带方向查询所有节点
match p=()-[]->()return p
查询出两个节点然后添加关系
match(n:test123) where n.code='123'match(m:test345) where m.code='345'create(n)-[r:role{name:'同事'}]->(m)MATCH(n:tupu1bb26267a36140528800d041a6ced5bc) where n.code='e6e92167-6fed-4d01-9de6-78a8bb59a375'MATCH(f:tupu1bb26267a36140528800d041a6ced5bc) where f.code='5fe2854f-870b-413a-ad85-8dae8f16e0a9'create(n)-[J:Lddd]->(q:dddd)-[H:ddd]->(f)//查询两点之间的共同邻居MATCH(n:tupu1bb26267a36140528800d041a6ced5bc{code:'e6e92167-6fed-4d01-9de6-78a8bb59a375'})-[]-(m)-[]-(f:tupu1bb26267a36140528800d041a6ced5bc{code:'5fe2854f-870b-413a-ad85-8dae8f16e0a9'})return m
//查询多个语句后去重并分页CALL apoc.cypher.run("
MATCH p=(a:tupu1bb26267a36140528800d041a6ced5bc)--()return p LIMIT100UNIONMATCH p=(n:tupu1bb26267a36140528800d041a6ced5bc)return p LIMIT100
",{})yield value
return value.p
limit 100
修改属性标签
code1为新属性标签
code为老属性标签
match(n:FSD)set n.code1=n.code remove n.code
修改属性值
match(n:FSD) where n.code1='123'set n.name='张三'
修改标签
match(n:FSD)
remove n:FSDsetn:FSD123
查询没有关系的节点
match(n) where not(n)-[]-()return n
多标签写法
添加:
create(n:WWWED:DDFFFS:gggggg{name:'张杀杀杀'})
查询
match(n:WWWED:DDFFFS:gggggg) where n.name='张杀杀杀'return n
批量清空后添加属性(节点)MATCH(n:`刘伟豪1`)SET n={title:"刑事案件性质代字",name:"dssdsd",age:12}RETURN n
批量清空后添加属性(关系)MATCH p=()-[r:`刑事案件性质代字`]->()SET r={title:"刑事案件性质代字"}RETURN p
模糊查询
https://blog.csdn.net/weixin_44141284/article/details/122298181MATCH(person:Person)//遍历所有 PersonMERGE(city:City {name: person.bornIn })//如果不存在 出生地的城市,则创建MERGE(person)-[r:BORN_IN]->(city)//如果不存在关系则创建RETURN person.name, person.bornIn, city
MERGE(keanu:Person {name:'Keanu Reeves'})ONCREATESET keanu.created =timestamp()//不存在时执行ONMATCHSET keanu.lastSeen =timestamp()//已存在时执行RETURN keanu.name, keanu.created, keanu.lastSeen
match(a),(b) where id(a)=25 and id(b)=8merge(a)-[r:gogogo]->(b)//如果不存在 gogogo 关系则创建
on create set r.w =1//当关系不存在新增关系时,增加或修改其属性
on match set r.w =100//当关系已存在时,增加或修改其属性return a,b,r
match(a),(b) where id(a)=25 and id(b)=8merge(a)-[r:gogogo]->(b)//如果不存在 gogogo 关系则创建
on create set r.w =1//当关系不存在时,新增关系时,增加或修改其属性
on match set r.w =coalesce(r.w,0)+1//当关系已存在时,增加或修改其属性return a,b,r
merge(n:GGGFF{name:'张三'}) 存在则不创建,不存在则创建
merge(m:HHHH{name:'李四'}) 存在则不创建,不存在则创建
merge(n)-[r:DDDDD]->(m) 存在则不创建,不存在则创建
查询节点属性key
MATCH(n:Person{name:'Ernesto'})RETURNkeys(n)MATCH(n)OPTIONALMATCH(n)-[r]-()RETURN distinct keys(n),keys(r)
如果一次性多个修改语句需要创建事务最后提交整体事务
Transaction transaction = session.beginTransaction();//第一步删除节点有关的所有关系
transaction.run("match(n:"+label+")-[r]-() where n.nodeTypeCommon=$nodeTypeCommon and n.code=$code delete r",parameters("nodeTypeCommon", nodeTypeCommon,"code",code));//清除除了code和nodeTypeCommon以外的属性key
transaction.run("MATCH (n:"+label+") where n.nodeTypeCommon=$nodeTypeCommon and n.code=$code WITH n, [k in keys(n) where not k in [\"code\",\"nodeTypeCommon\"]] as keys CALL apoc.create.removeProperties(n, keys) YIELD node RETURN count(*)",parameters("nodeTypeCommon", nodeTypeCommon,"code",code));//重新添加为改节点添加属性key
transaction.run("MATCH (n:"+label+") where n.nodeTypeCommon=$nodeTypeCommon and n.code=$code SET n="+values+" RETURN n",parameters("nodeTypeCommon", nodeTypeCommon,"code",code));//新增关系for(int i =0;i<relationship.size();i++){
String relationshipAttr = relationship.getJSONObject(i).getStr("relationshipAttribute");
String startCode = relationship.getJSONObject(i).getStr("startCode");
String endCode = relationship.getJSONObject(i).getStr("endCode");
String relationshipAttribute = relationshipAttr.replaceAll("\"(\\w+)\"(\\s*:\\s*)","$1$2");
transaction.run("MATCH (n) WHERE n.code = $startCode and n.nodeTypeCommon=$nodeTypeCommon \n"+"MATCH (m) WHERE m.code = $endCode and m.nodeTypeCommon=$nodeTypeCommon \n"+"create(n)-[r:relationship"+relationshipAttribute+"]->(m)",parameters("startCode", startCode,"endCode",endCode,"nodeTypeCommon",nodeTypeCommon));}//手动提交事务
transaction.commit();