java多线程    Java入门    vsftp    ftp    linux配置    centos    FRP教程    HBase    Html5缓存    webp    zabbix    分布式    neo4j图数据库    

07. Neo4j CQL WHERE 语法

Neo4j CQL - WHERE子句

为了演示条件 先搞几个节点

CREATE (stu:Student{id:1,name:"Jim",age:18,grade:2})
CREATE (stu:Student{id:2,name:"Tom",age:19,grade:3})
CREATE (stu:Student{id:3,name:"Lucy",age:17,grade:1})
CREATE (stu:Student{id:4,name:"LiLi",age:17}) 
CREATE (stu:Student{id:5,name:"James",age:19,grade:3,email:'james@java-er.com'}) 

Neo4j CQL WHERE 查询一个属性

MATCH (stu:Student) where stu.name='Jim' return stu

Neo4j CQL WHERE 查询的OR 或

MATCH (stu:Student) where stu.name='Jim' or stu.name='Lucy' return stu

除了OR还有 AND XOR NOT

Neo4j CQL WHERE 查询的比较大小

MATCH (stu:Student) where stu.age>=18 return stu

Neo4j CQL WHERE IN查询

MATCH (a:Student) WHERE a.name IN ['Jim', 'Tom'] RETURN a.name,a.age

范围查询

MATCH (a:Student) WHERE a.age>18 and a.age<20 return a

Neo4j CQL WHERE 查询属性不丢失的数据

MATCH (stu:Student) where stu.grade is not null return stu

Neo4j CQL WHERE WITH和MATCH联合使用

MATCH (stu:Student) WITH stu.name as name WHERE stu.age = 18 RETURN name

Neo4j CQL WHERE String 匹配

匹配名字开头为L的学生

MATCH (stu:Student) WHERE stu.name STARTS WITH 'L' RETURN stu

匹配名字结尾为y的学生

MATCH (stu:Student) WHERE stu.name ENDS WITH 'y' RETURN stu

匹配名字包含i的学生

MATCH (stu:Student) WHERE stu.name CONTAINS 'i' RETURN stu

不的情况举例子,不是y结尾的学生

MATCH (stu:Student) WHERE NOT stu.name ENDS WITH 'y' RETURN stu

Neo4j CQL WHERE 正则表达式=~

MATCH (stu:Student) WHERE stu.name =~ 'Ji.*' RETURN stu

正则表达式逃离

MATCH (stu:Student) WHERE stu.email =~ '.*\\.com' RETURN stu

正则表达式,不要区分大小写

MATCH (stu:Student) WHERE stu.name =~ '(?i)JA.*' RETURN stu.name, stu.age

Neo4j CQL 查询和一个特定节点有关系的节点

下面打造关系

image-20221020153011808

清空数据库

MATCH (n) DETACH DELETE n

批量执行

//创建一个关系,Tom Love Luna
CREATE (:Person{name:'Tom'})-[:Love]->(:Person{name:'Luna'})

//创建一个关系,Jim LIKES Lucy
CREATE (:Person{name:'Jim'})-[:LIKES]->(:Person{name:'Lucy'})

//创造一个黑狗
CREATE (:Dog{color:'black'})

以下语句,一条一条在浏览器里执行

//让Jim拥有这条黑狗
MATCH (p:Person),(dog:Dog) where p.name='Jim' and dog.color='black'
CREATE (p)-[r1:has_dog]->(dog) return r1

//查到Lucy让她拥有一条新创造的白狗
MATCH (p:Person) where p.name='Lucy'
CREATE (p)-[r2:has_dog]->(dog:Dog{color:'white'}) return r2

//给Lucy的白狗一个玩具
MATCH (dog:Dog)where dog.color='white'
CREATE (dog)-[r3:has_toy]->(toy:Toy{name:'apple'}) return r3

Neo4j CQL WHERE 查询关系

“查询和Lucy这个人,有关系的节点”

MATCH  (lucy:Person {name: 'Lucy'}),  (other:Person) WHERE (other)-->(lucy) RETURN other.name
╒════════════╕
│"other.name"│
╞════════════╡
│"Jim"       │
└────────────┘

Neo4j CQL WHERE 查询没有关系NOT

没有关系的情况NOT

MATCH  (lucy:Person {name: 'Lucy'}),  (other:Person) WHERE NOT (other)-->(lucy) RETURN other.name
╒════════════╕
│"other.name"│
╞════════════╡
│"Tom"       │
├────────────┤
│"Luna"      │
├────────────┤
│"Lucy"      │
└────────────┘

Neo4j CQL WHERE 查询特定关系 - 不指名方向只指名关系

MATCH (n:Person) WHERE (n)-[:LIKES]-({name: 'Jim'}) RETURN n
╒═══════════════╕
│"n"            │
╞═══════════════╡
│{"name":"Lucy"}│
└───────────────┘

Neo4j CQL WHERE 查询特定关系 - 指名方向和关系

MATCH (n:Person) WHERE (n)-[:LIKES]->({name: 'Jim'}) RETURN n
MATCH (n:Person) WHERE (n)-[:LIKES]->({name: 'Lucy'}) RETURN n
╒══════════════╕
│"n"           │
╞══════════════╡
│{"name":"Jim"}│
└──────────────┘

我们发现第一个语句查不到任何人,第二个可以查到 Jim

查询关系类型,正则匹配,所有L开头关系的都查出来,这样Like和Love都出来了

MATCH (n:Person)-[r]->()
WHERE type(r) =~ 'L.*'
RETURN type(r), r
╒═════════╤═══╕
│"type(r)"│"r"│
╞═════════╪═══╡
│"Love"   │{} │
├─────────┼───┤
│"LIKES"  │{} │
└─────────┴───┘

Neo4j CQL WHERE 中的子查询

查询有狗的人

MATCH (p:Person) WHERE EXISTS {  MATCH (p)-[:has_dog]->(:Dog) } RETURN p.name as name    
╒══════╕
│"name"│
╞══════╡
│"Jim" │
├──────┤
│"Lucy"│
└──────┘

查询嵌套,查询有黑狗的那个人

MATCH (p:Person) WHERE EXISTS {  MATCH (p)-[:has_dog]->(dog:Dog)  WHERE dog.color = 'black' } RETURN p.name AS name
╒══════╕
│"name"│
╞══════╡
│"Jim" │
└──────┘

查询有狗,且他的狗有玩具的那个人

MATCH (p:Person) WHERE EXISTS {  MATCH (p)-[:has_dog]->(dog:Dog)  WHERE EXISTS {    MATCH (dog)-[:has_toy]->(toy:Toy)    WHERE toy.name = 'apple'  } } RETURN p.name AS name
╒══════╕
│"name"│
╞══════╡
│"Lucy"│
└──────┘

将where条件,写在MATCH内的查询模式

MATCH (p:Person) WHERE EXISTS {  MATCH (p)-[:has_dog]->(dog:Dog)  WHERE EXISTS {    MATCH (dog)-[:has_toy]->(toy:Toy{name:'apple'})        }} RETURN p.name AS name
╒══════╕
│"name"│
╞══════╡
│"Lucy"│
└──────┘

Neo4j CQL WHERE 查询返回关系节点对象

MATCH (a:Person {name: 'Jim'}) RETURN [(a)-->(b WHERE b:Person) | b.name] AS friends
╒═════════╕
│"friends"│
╞═════════╡
│["Lucy"] │
└─────────┘


This entry was posted in 数据库, 高并发与大数据 and tagged . Bookmark the permalink.
月小升QQ 2651044202, 技术交流QQ群 178491360
首发地址:月小升博客https://java-er.com/blog/neo4j-cql-where/
无特殊说明,文章均为月小升原创,欢迎转载,转载请注明本文地址,谢谢
您的评论是我写作的动力.

Leave a Reply