cmc
cmc

Reputation: 1107

neo4j/cypher: Find all nodes adjacent to all nodes in some set

Given a node, s, adjacent to a set, C, of >1 nodes of some label. I want to find all nodes that are also adjacent to every element in C.

More generally:

Is this even doable in cypher?

Upvotes: 1

Views: 807

Answers (1)

Dave Bennett
Dave Bennett

Reputation: 11216

Yes, absolutely, this is doable in cypher. Here is an example.

// start with finding the start node 's' and the adjacent nodes
match (s:Node {name: 's'} )-[:ADJ]->(C:Node)
// match only the adjacent nodes that have 'set C'
where C.set = 'C'
// pass C onto the remainder of the query
with s,C
// match the nodes that are adjacent to the nodes in 'set C'
match C-[:ADJ]->(adjacent)
// return all of the nodes in set C and a collection of the nodes
// adjacent to the set C nodes
return s.name, C.name, collect(adjacent.name)

Upvotes: 1

Related Questions