- 查询John的朋友的朋友
添加5个人名节点,名字分别为
CREATE (:People{name:"John"})
CREATE (:People{name:"Joe"})
CREATE (:People{name:"Sara"})
CREATE (:People{name:"Steve"})
CREATE (:People{name:"Maria"})
此时查询所有人的结果如下
match (people:People) return people
给这5个人添加上朋友关系
MATCH (a:People{name:"John"}) MATCH (b:People{name:"Joe"}) CREATE (a) -[:friend]->(b)
MATCH (a:People{name:"John"}) MATCH (b:People{name:"Sara"}) CREATE (a) -[:friend]->(b)
MATCH (a:People{name:"Joe"}) MATCH (b:People{name:"Steve"}) CREATE (a) -[:friend]->(b)
MATCH (a:People{name:"Sara"}) MATCH (b:People{name:"Maria"}) CREATE (a) -[:friend]->(b)
再次查询
match (people:People) return people
查询John朋友的朋友的名字
match (john {name:'John'})-[:friend]->()-[:friend]->(fof) return john.name,fof.name
结果
因为这里查询的只是名字,而不是节点,所以只会显示各自的名称。
给定一个用户名列表,找到名字在列表中的所有节点。匹配他们的朋友,仅返回那些他们关注的name属性以'S'开头的用户
match (user)-[:friend]->(follower) where user.name in ['Joe','John','Sara','Maria','Steve'] and follower.name =~'S.*' return user.name,follower.name
结果
获取John和他的朋友数量
match (n {name:'John'})-[:friend]->(friend) with n,count(friend) as friendsCount return n,friendsCount
获取John的朋友数量并保存为John的一个属性
match (n {name:'John'})-[:friend]->(friend) with n,count(friend) as friendsCount set n.friendsCount=friendsCount return n.friendsCount
- 查询朋友的朋友,不包含自己
添加3个用户,两个好友关系
create (adam:User {name:'Adam'}),(pernilla:User {name:'Pernilla'}),
(david:User {name:'David'}),(adam)-[:friend]->(pernilla),
(pernilla)-[:friend]->(david)
查询结果
match (n:User) return n
match (user {name:'Adam'})-[:friend]->()-[:friend]->(friend) return friend
match (user {name:'Adam'})-[:friend]->(friend),(friend)-[:friend]-(alluser) return alluser
该方式也是不包含自己的,可以与下面包含自己的做出比较。
查询朋友的朋友,包含自己
match (user {name:'Adam'})-[:friend]->(friend) match (friend)-[:friend]-(alluser) return alluser