Krishna Shetty
Krishna Shetty

Reputation: 1441

How to find out connection between a set of nodes?

I have a scenario where I know IDs of a list of nodes. I need to get connection(if exists) between these nodes given their IDs. Is there any way to achieve this? Update: I am using node id property not the neo4j's internal ID(using like match (n:Person{id:3}))

Upvotes: 1

Views: 82

Answers (2)

JohnMark13
JohnMark13

Reputation: 3739

You can use the IN clause to select from a list of values:

MATCH (n)-[r*..2]-(m)
WHERE ID(n) IN [0,1,2] AND ID(m) IN [2,3,4]
RETURN r

I've limited the path length to 2 hops of indeterminate relationship type here, and arbitrarily picked some IDs.

To return the path instead:

MATCH p=(n)-[r*..2]-(m)
WHERE ID(n) IN [0,1,2] AND ID(m) IN [2,3,4]
RETURN p

Upvotes: 3

ulkas
ulkas

Reputation: 5918

START n=node(1,2,3,4,5,6) //your IDs of a list of nodes
MATCH p=n-[r]-m  //the connection for 1 hop. for multiple hops do n-[r*]-m
WHERE Id(m) in [1,2,3,4,5,6] //your IDs of a list of nodes
RETURN p

Upvotes: 2

Related Questions