Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
04-19-2021 01:30 AM
I am working on an app which uses NEO4J. There are mainly two types of Nodes
For example,
Need some guidance on how this can be achieved using CYPHER query
What I'm trying to do
MATCH(n:User)-[r:FRIENDS_WITH]-(u1:User) where n.name = 'A' with u1 match(s:Post)-[r1:OWNED_BY]-(u1) UNION (Trying to figure out how to select Post which are not in s ) I'm trying to address based on my SQL knowledge. I'm getting posts of all users who are FRIENDS_WITH 'A' and after this I want to do a UNION of missing posts, but unable to figure this out
Please do let me know if there's a better way of achieving the outcome. Thank you so much
Solved! Go to Solution.
04-19-2021 02:36 AM
Like this, you should get your friend posts first then posts of other users
MATCH (a:User)-[:FRIENDS_WITH]-(b:User)-[:OWNED_BY]-(p:Post)
WHERE a.name = 'A'
WITH collect(p) AS posts, collect(b) + a AS users
MATCH (u:User)-[:OWNED_BY]-(p:Post)
WHERE u NOT IN users
RETURN posts + collect(p) AS posts
04-19-2021 02:11 AM
Hello @vader.219k
This query should solve your use case?
MATCH (a:User)-[:FRIENDS_WITH]-(:User)-[:OWNED_BY]-(p:Post)
WHERE a.name = 'A'
RETURN p
Regards,
Cobra
04-19-2021 02:24 AM
Hi Cobra,
Thank you so much for your response.
Unfortunately this doesn't solve my problem because the above only gives me all the posts which are created/owned by a particular user friend's circle.
What I am looking for is to have a list of all posts available in the DB , I want to display my friends posts first followed by others.
04-19-2021 02:36 AM
Like this, you should get your friend posts first then posts of other users
MATCH (a:User)-[:FRIENDS_WITH]-(b:User)-[:OWNED_BY]-(p:Post)
WHERE a.name = 'A'
WITH collect(p) AS posts, collect(b) + a AS users
MATCH (u:User)-[:OWNED_BY]-(p:Post)
WHERE u NOT IN users
RETURN posts + collect(p) AS posts
04-19-2021 03:46 AM
Hi Cobra,
Thank you so much for helping me out. Just a minor syntax error in the above and also modified query to include user 'A' posts as well. Also did an UNWIND to get back the original rows
MATCH (a:User)-[:FRIENDS_WITH]-(b:User)-[:OWNED_BY]-(p:Post)
WHERE a.name = 'A'
WITH collect(p) AS posts, collect(b) AS users
MATCH (u:User)-[:OWNED_BY]-(p:Post)
WHERE NOT u IN users
WITH posts + collect(p) AS allposts
unwind allposts as post
return post
All the sessions of the conference are now available online