Allen Lu
Allen Lu

Reputation: 105

Neo4j Check property cypher

I have a group of nodes like the example image below. Is there a cypher query that can check if a node is turned "off" or "on" (in green); if it is "on" it returns the node the "on" node connects to and the original node that connects to "on". If it node is "off" it just returns the node that connects to it. In this example It should return label1 and label2, but not label3.

example

Upvotes: 0

Views: 80

Answers (2)

joe
joe

Reputation: 1911

I am assuming that 'on' and 'off' are states on a parameter. I am going to call it State in the code. I am also assuming that you want to start at a specific node. I am not sure how you get this node so I will assume you have the ID and it equals 1

MATCH (a)-[r:connect*..]->(b) where ID(n) =1 b.State='on' return b

Upvotes: 0

stdob--
stdob--

Reputation: 29167

You can use combination of optional match and with as the union:

// Is the starting node
MATCH (S {name: 'label1'})
WITH S
  // Get the nodes to which have access through a pattern of "ON"
  OPTIONAL MATCH (S)-[:connect]->({on: true})-[:connect]->(onC)
WITH S, collect(distinct onC) as onConnect
  // Get the nodes to which have access through a pattern of "OFF"
  OPTIONAL MATCH (S)-[:connect]->({on: false})<-[:connect]-(offC)
WITH S, onConnect, collect(distinct offC) as offConnect
  // Remove possible duplicates
  UNWIND S + onConnect + offConnect as N
RETURN collect(distinct N) as result

Upvotes: 1

Related Questions