Excluding some of the returned MATCH query results in Neo4J
I am using the MATCH query below (refer to book viewers):
MATCH (u:User {id:15})-[r:REVIEW]->(b:Book)
WITH u,b
MATCH (t:User)-[r:REVIEW]->(b)
RETURN distinct t
This cypher first matches the books viewed by the user with id = 15. Then it uses another MATCH command to find any other user who has viewed any book that was checked by the user with id = 15. However, the returned users include the user with id = 15. How can I exclude it?
Note that I used the "detached" command so that I don't get the same user 2 or more times. FE if a user (id = 15) has viewed 2 books and another user has viewed these 2 books, I would get the last user 2 times without the "separate" ones.
source to share
Another option is to capture what you want in a single template, for example:
MATCH (u:User{id:15})-[:REVIEW]->(b:Book)<-[:REVIEW]-(t:User)
RETURN distinct t
If you have one suggestion and a MATCH pattern, since we distinguish between variables u
and t
, it will never match t
- u
.
If you want to use a shorthand version (assuming: book nodes are the only things that: Users can: BROWSE and that: users are only the types of nodes that can: BROWSE: Books), then you can shorten that to
MATCH (u:User{id:15})-[:REVIEW*2]-(t:User)
RETURN distinct t
source to share