cancel
Showing results forΒ
Did you mean:Β

## 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
Ninja

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

Graph Fellow

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.