Return my friends and friends of friends using Neo4j's Cypher

大兔子大兔子 提交于 2019-12-11 03:37:54

问题


I have nodes with bi-relations(follow)

I am trying to create a query which I want to return all following nodes of a specific node and the following nodes of his following nodes(3 hopes depth)

For example assume those relations:

-> symbols follow

A->B
B->A
B->C
C->B
C->D
D->C
C->E
E->C

I am expecting to get this response if I execute the query on node A

B C D E

I tried this:

MATCH (user:User {name:'roi'})-[:nearby*1..3]->(foaf)
WHERE NOT((user)-[:nearby]->(foaf))
RETURN user, foaf

the problem is that I dont get the node (A) followings only the followings of his followings friends

So I got

C D E

I didnt get B

anyone could help? thanks


回答1:


You don't get B because your WHERE pattern filters out any node that is [:NEARBY] to A. For that particular relationship it is like saying

MATCH A-[:NEARBY]->B
WHERE NOT(A-[:NEARBY]->B)

Edit

If you want a users' friends and friends of friends to depth three but not the user you can do

MATCH (user:User {name:'roi'})-[:NEARBY*1..3]->f
WHERE f <> user
RETURN f


来源:https://stackoverflow.com/questions/32503350/return-my-friends-and-friends-of-friends-using-neo4js-cypher

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!