Cypher - [OPTION] MATCH

条件表达式

CASE test
WHEN value THEN result
[WHEN ...]
[ELSE default]
END

CASE
WHEN
predicate THEN result
[WHEN ...]
[ELSE default]
END

 match(n:User)
where n.id = 1
return case n.id when 1 then '管理员' else '普通人员' end as result

match(n:User)
where n.id = 1
return case  when n.id=1 then '管理员' else '普通人员' end as result

// 查询所有节点

match (n)
return n

//节点和属性过滤

match (me)
where me.id = 1 and me:User
return me

//检查【节点】【属性】的存在性

match (me)
where exists(me.age)
return me

//字符串匹配(区分大小写)

match (u)
where u.name starts with 'a' and u.name ends with 'z' or u.name contains 'zs' 
return u

// 匹配多种关系中的一种 , 使用  |

match (n) -[r :ORG | :DEP]-> ()
return n

// 返回类型类型

match (n) -[r]- ()
return type(r)

//返回标签类型

match (n:User)
where n.id = 1
return labels(n)

//返回多个类型 【我朋友的朋友】

match (me:User) -->(friend:User) --> (friend_of_friend:User)
return  friend_of_friend

可变长关系   -[:TYPE * minHops .. maxHops]->

// 返回 1 至 2 跳的以内的用户

match (me:User{id:1})  -[*1..2]->(n:User)
return n

// 返回 0跳用户,就是返回自己

match (me:User{id:1})  -[*0]->(n:User)
return n

//返回至少有 2跳的用户

match (me:User)  -[*2..]-> (n:User)        // -[*2..]-> 等于 -[*2]->
return me     

//返回小于 2跳的用户, 注意不包含 0跳用户, 如果要包含应该是  -[*0..2]->

match (me:User)  -[*..2]->(n:User)            // -[*..2]-> 等于 -[*1..2]->
return me       

// 返回数据库中节点的id。 注意neo4j 会重用已经删除的节点的id

match (me:User)
where me.id = 3
return id(me)

返回指定用户的所有关系 

match (u1:User) -[*]-> (u2:User)
where u1.id = 1                     
// -[*]-> 表示跳数不限制
return *           

match (u1:User) --> (u2:User) where u1.id = 1 return *                     //只表示返回用户本身,及其1跳的用户

返回找到指定用户,返回跳数大于2的路径

match p=(u1:User)-[*2]->(u2:User)
where u1.id = 1
return p

// 指定两个节点,找出其最短路径

match (u1:User),(u2:User),sp = shortestPath((u1)-[*]->(u2))
where u1.id = 1 and u2.id = 3  
return sp

//带断言的最短路径

 

//所有最短路径

match (u1:User),(u2:User),p = allShortestPaths((u1) -[*]-> (u2) )
where u1.id = 1 and u2.id = 3  
return p

//模式过滤,注意 where 中的模式是过滤 match 之后的结果

match (u),(o)
where u.id in [1,2] and not (u)-->(o)
return *

optinal match

用于搜索模式中描述的匹配项,对于找不到的项目用null 代替

match (me:User)
where me.id = 32
optional match (me)-->(o) 
return o

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://my.oschina.net/u/2552286/blog/3070327

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值