cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to select all nodes between 2 nodes exept start and end nodes?

Hi guys! The question is how to get all nodes in path between start and end note

match p=((s:Group{id:10})-[:CHILD*]->(f:Group{id:1})) return p

how exclude nodes with id 10 and id 1 from the result?

5 REPLIES 5

Hi @alexander.kaluzhskiy

This is my data.

CREATE (s:Group{id:10})-[:CHILD]->(:Group{id:2})-[:CHILD]->(:Group{id:3})-[:CHILD]->(f:Group{id:1}),
       (s)-[:CHILD]->(:Group{id:4})-[:CHILD]->(:Group{id:5})-[:CHILD]->(f),
       (s)-[:CHILD]->(:Group{id:6})-[:CHILD]->(f)

I create the Cypher.
It works, but it's not cool.

MATCH p=((s:Group{id:10})-[:CHILD*]->(f:Group{id:1}))
WITH nodes(p) AS nodes
UNWIND nodes AS node
WITH collect(distinct(node.id)) AS allNodeIds
MATCH (g:Group)
  WHERE g.id IN allNodeIds
  AND NOT g.id IN [10,1]
RETURN g

Thank you so much, I will try to reuse your solution for my task

Bennu
Graph Fellow

Hi @alexander.kaluzhskiy !

Based on @koji solution I may just add one modification in order to avoid some 'branching' on the profile of the query.

explain MATCH p=((s:Group{id:10})-[:CHILD*]->(f:Group{id:1}))
WITH nodes(p) AS nodes
UNWIND nodes AS node
with node where NOT node.id IN [10,1]
RETURN node

Bennu

PS : let us know if APOC is an option for you as well.

Wow, thanks. This query is way understandable. I just started to use neo4j, and I looking for query I able to understand

@Bennu Cool, I like this query!