Neo4j Cypher语言

neo4j模糊查询

我与Neo4j用户一起工作时经常看到的一种建模方法是创建非常通用的关系(例如HAS,CONTAINS,IS),并在关系属性或端节点的属性/标签上进行过滤。

——-----------------

create (x:Node {属性名:‘属性值’})
match (x),(y) where x.属性=‘’ and y.属性=‘属性值’ create (x)-[r:关系名{关系属性:属性值}]->(y)
CALL db.labels()
match (x) where x.属性 contains ‘’ return x

标题原文链接:https://www.cnblogs.com/mzyn22/p/11098285.html

一、添加操作

1. 添加节点:

create (x:学生{studentId:‘1001’,age:20}

2. 添加关系:

对现有的节点添加关系

match (x:学生{studentId:1001}),(y:教师{tid:‘09’}) create (x)-[jx:课程{name:‘高数’}]->(y)

match (x:学生),(y:教师) where x.studentId=‘1001’ and y.tid=‘09’ create (x)-[jx:课程{name:‘高数’}]->(y)

添加节点并添加关系

create (x:学生{studentId:1001})-[jx:课程{name:‘高数’}]->(y:教师{tid:‘09’})

3. 根据节点的ID创建关系

match (x:学生),(y:教师) where id(x)=254885 and id(y)=554896 create (x)-[jx:课程{name:‘高数’}]->(y)

二、删除操作

1. 删除节点:

match (x:学生{studentId:1001}) delete x

2. 删除关系:

match (x:学生{studentId:1001})-[jx:课程{name:‘高数’}]->(y:教师{tid:‘09’}) delete jx

3. 删除关系的同时,删除数据:

match (x:学生{studentId:1001})-[jx:课程{name:‘高数’}]->(y:教师{tid:‘09’}) delete x,jx,y

三、修改节点

1. 给节点添加一个新的属性,两种方式:

match(x:学生{studentId:‘1001’}) set x.age=21 return x

match(x:学生{studentId:‘1001’}) set x+={age:21} return x

2. 给节点添加属性并删除现有属性

match(x:学生{studentId:‘1001’}) set x={age:21,name:‘abc’} //注意这里,会将studentId属性删除

3. 添加新标签:

match(x:学生{studentId:‘1001’}) set x:男生 return x //添加一个标签

match(x:学生{studentId:‘1001’}) set x:男生:团员 return x //添加多个标签

四、查询操作

1. 根据节点属性查找对应节点:

match(x:Student{studentId:‘1001’}) return x

或者

match(x:Student) where x.studentId=‘1001’ return x

2. 根据关系查找节点

match (x)-[r:教学内容]-(y) where r.课程=‘语文’ return x,r,y

3. 查询单独的节点,即:与其他任何节点没有任何关系

match(x) where not (x)-[]-() return x

4. 查询N层关系的节点:

match q=(x)-[*5…8]-() return q limit 200 这个为查询5到8层关系的

match q=(dh)-[r]-(jq)-[rr]-()-[]-()-[]-()-[]-()-[]-()-[]-() return q limit 400

5. 查询节点关系数个数:

match(dh:学生)-[r]-(jq:老师) with dh, count® as dhs where dhs > 2 return dh

6. 查询节点个数:

match(x) return count(x)

7. 查询所有的关系类型:

CALL db.relationshipTypes()

8. 查询所有的节点标签:

CALL db.labels()

9. 查询节点关系种类:

CALL db.schema()

10. 最短路径查询

MATCH (x:电话{hm:“02711111111”}),(y:电话{sjdbh:“025111111111”}),p=shortestpath((x)-[*…10]-(y))RETURN p

11. 查询不存在某个属性的节点

match(x:电话) where x.repeat is null with x match p=(x)-[r*1…5]-(y) return p

12. 查询存在关联到某一个节点具有相同属性的其他的节点

match p=(x)-[]-()-[]-(y) where x.name=y.name return p

13. 查询两个节点之间存在多个关系的节点

match p=(x)-[r1]-(y)-[r2]-(x) where id(r1)<>id(r2) return p

14. 查询某一个节点具有m到n层关系的所有的节点

match q=(x:学生)-[*1…5]-() where x.no =‘201921011XXXX’ return q #查询某学生有关系的节点,最多五层关系

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YingJingh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值