简体   繁体   中英

Neo4j filtering nodes with only one type of relationship

I'm new with neo4j and I'm stuck in this exercise. I have to find the names and cities of the students who only have friends in their city. The graph is something like this: 在此处输入图片说明

The name of the relationship is "FRIEND_OF", and the color represents the city.

Thanks so much.

As the city property is stored on the node, you can add a WHERE clause :

MATCH (user:User)
MATCH (user)-[:FRIEND_OF]-(friend)
WHERE user.city = friend.city
RETURN user.name, collect(distinct(friend)) as friends

EDIT If you want to return users where all his friends lives in the same city, try this one :

MATCH (user:User)-[:FRIEND_OF]->(friend)
WITH user, collect(friend) AS friends
WHERE ALL (x IN friends 
           WHERE x.city = user.city)
RETURN user

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM