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?

Hi @alexander_kaluz

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

1 Like

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

1 Like

Hi @alexander_kaluz !

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.

3 Likes

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!