//过滤聚合值,读 WITH 聚合 WHERE
//返回和David联系的至少一次外连接的人
MATCH (david { name: 'David' })--(otherPerson)-->()
WITH otherPerson, count(*) AS foaf
WHERE foaf > 1
RETURN otherPerson.name
//在collect之前对数据进行排序和限制
// 先读,WITH,排序
MATCH (n)
WITH n
ORDER BY n.name DESC LIMIT 3
RETURN collect(n.name)
//限制下一部分的搜索范围
MATCH (n { name: 'Anders' })--(m)
WITH m
ORDER BY m.name DESC LIMIT 1
MATCH (m)--(o)
RETURN o.name
UNWIND
UNWIND
解开List成为一行
这个list可能是collect
处理的
UNWIND [1, 2, 3, NULL ] AS x
RETURN x, 'val' AS y
WITH [1, 1, 2, 2] AS coll
UNWIND coll AS x
WITH DISTINCT x
RETURN collect(x) AS setOfVals
// 展开任何返回list的表达式
WITH [1, 2] AS a,[3, 4] AS b
UNWIND (a + b) AS x
RETURN x
// 展开list的list
WITH [[1, 2],[3, 4], 5] AS nested
UNWIND nested AS x
UNWIND x AS y
RETURN y
//应用空list,返回空
UNWIND [] AS empty
RETURN empty, 'literal_that_is_not_returned'
// 为了不处理空list,使用CASE
WITH [] AS list
UNWIND
WHEN list = []
THEN [null]
ELSE list
END AS emptylist
RETURN emptylist
UNWIND NULL AS x
RETURN x, 'some_literal'
// 通过参数创建节点
"events" : [ {
"year" : 2014,
"id" : 1
"year" : 2014,
"id" : 2
UNWIND $events AS event
MERGE (y:Year { year: event.year })
MERGE (y)<-[:IN]-(e:Event { id: event.id })
RETURN e.id AS x
ORDER BY x
WHERE
WHERE是MATCH, OPTIONAL MATCH and WITH
语句的一部分
WHERE变量使用index可以提高查询性能
以下面graph为例:
// 创建节点
CREATE (andy:Swedish:Person {name: 'Andy',belt:'white',age:36})
CREATE (peter:Person {name: 'Peter',email:'peter_n@example.com',age:35})
CREATE (timothy:Person {name: 'Timothy',address:'Sweden/Malmo',age:25})
CREATE (fido:Dog {name: 'Fido'})
CREATE (ozzy:Dog {name: 'Ozzy'})
CREATE (andy_dog:Dog {name: 'Andy'})
CREATE (banana:Toy {name: 'Banana'})
// 创建节关系
CREATE (andy)-[:HAS_DOG{since:2016}]->(andy_dog)
CREATE (andy)-[:KNOWS{since:1999}]->(peter)
CREATE (andy)-[:KNOWS{since:2012}]->(timothy)
CREATE (peter)-[:HAS_DOG{since:2010}]->(fido)
CREATE (peter)-[:HAS_DOG{since:2018}]->(ozzy)
CREATE (fido)-[:HAS_TOP]->(banana)
//AND, OR, XOR and NOT
MATCH (n:Person)
WHERE n.name = 'Peter' XOR (n.age < 30 AND n.name = 'Timothy') OR NOT (n.name = 'Timothy' OR n.name = 'Peter')
RETURN n.name, n.age
//过滤节点标签
MATCH (n)
WHERE n:Swedish
RETURN n.name, n.age
//过滤节点属性
MATCH (n:Person)
WHERE n.age < 30
RETURN n.name, n.age
//过滤关系属性
MATCH (n:Person)-[k:KNOWS]->(f)
WHERE k.since < 2000
RETURN f.name, f.age, f.email
//动态计算属性
WITH 'AGE' AS propname
MATCH (n:Person)
WHERE n[toLower(propname)]< 30
RETURN n.name, n.age
//属性是否存在
MATCH (n:Person)
WHERE EXISTS (n.belt)
RETURN n.name, n.belt
字符串匹配
STARTS WITH and ENDS WITH and ONTAINS
,大小写敏感
// STARTS WITH
MATCH (n:Person)
WHERE n.name STARTS WITH 'Pet'
RETURN n.name, n.age
//ENDS WITH
MATCH (n:Person)
WHERE n.name ENDS WITH 'ter'
RETURN n.name, n.age
//CONTAINS
MATCH (n:Person)
WHERE n.name CONTAINS 'ete'
RETURN n.name, n.age
MATCH (n:Person)
WHERE NOT n.name ENDS WITH 'y'
RETURN n.name, n.age
Cypher支持正则表达式,Cypher正则继承与Java的正则
支持改变字符串匹配模式:case-insensitive (?i), multiline (?m) and dotall (?s)
忽略大小写:MATCH (n) WHERE n.name =~ '(?i)Lon.*' RETURN n
使用正则表达式:=~ 'regexp'
MATCH (n:Person)
WHERE n.name =~ 'Tim.*'
RETURN n.name, n.age
//某些字符不使用正则
// . or * 使用转义字符
MATCH (n:Person)
WHERE n.email =~ '.*\\.com'
RETURN n.name, n.age, n.email
//大小写不敏感
MATCH (n:Person)
WHERE n.name =~ '(?i)AND.*'
RETURN n.name, n.age
//有外连接到timothy的人,WHERE中加入图模式
MATCH (timothy:Person { name: 'Timothy' }),(other:Person)
WHERE other.name IN ['Andy', 'Peter'] AND (timothy)<--(other)
RETURN other.name, other.age
//模式中使用NOT不匹配
MATCH (person:Person),(peter:Person { name: 'Peter' })
WHERE NOT (person)-->(peter)
RETURN person.name, person.age
//add properties to your patterns
MATCH (n:Person)
WHERE (n)-[:KNOWS]-({ name: 'Timothy' })
RETURN n.name, n.age
//过滤关系类型
MATCH (n:Person)-[r]->()
WHERE n.name='Andy' AND type(r)=~ 'K.*'
RETURN type(r), r.since
WHERE中使用子查询
子查询也可以用于指定模式是否存在
子查询使用{}
子查询可以使用外部的变量
subquery was introduced in Neo4j 4.0
- 语法格式:
EXISTS {
MATCH [Pattern]
WHERE [Expression]
//一般的子查询
MATCH (person:Person)
WHERE EXISTS {
MATCH (person)-[:HAS_DOG]->(:Dog)
RETURN person.name as name
//子查询中加入WHERE
MATCH (person:Person)
WHERE EXISTS {
MATCH (person)-[:HAS_DOG]->(dog :Dog)
WHERE person.name = dog.name
RETURN person.name as name
//子查询嵌套
MATCH (person:Person)
WHERE EXISTS {
MATCH (person)-[:HAS_DOG]->(dog:Dog)
WHERE EXISTS {
MATCH (dog)-[:HAS_TOY]->(toy:Toy)
WHERE toy.name = 'Banana'
RETURN person.name as name
WHERE中使用list
//判断是否在list中
MATCH (a:Person)
WHERE a.name IN ['Peter', 'Timothy']
RETURN a.name, a.age
//却失的值为null,在判断是为false
MATCH (n:Person)
WHERE n.belt = 'white'
RETURN n.name, n.age, n.belt
//IS NULL
MATCH (n:Person)
WHERE n.belt = 'white' OR n.belt IS NULL RETURN n.name, n.age, n.belt
ORDER BY n.name
WHERE中使用范围
MATCH (a:Person)
WHERE a.name >= 'Peter'
RETURN a.name, a.age