Only matching nodes with a definitive start and end point

I have a large neo4j graph dataset, where I am trying to get all the nodes present along the path from node A to be node B, which would include the connecting nodes in-between as well as A and B(Let's give the label for these nodes between A and B 'T'). However, when using this query:

MATCH (from:A{ name:'NameA'} ) CALL apoc.path.subgraphNodes(from, { labelFilter:"T|B"} ) YIELD node RETURN node

I get all the nodes that point to B, including those that don’t originate from A. Any help would be appreciated.

You could try this:

MATCH path = (from:A {name:'A'})-[*1..]->(to:B)
WITH nodes(path) as nn
UNWIND nn as n

Typically the graph is faster if you define the REL being traversed though if it's unique.


MATCH path = (a:A)-[*0..]->(b:B)
RETURN nodes(path)