Neo4j检查属性密码
问题描述:
我有一组像下面的示例图像的节点。是否有一个密码查询可以检查一个节点是“关闭”还是“开启”(绿色);如果它是“开”,则返回“开”节点连接的节点和连接到“开”的原节点。如果节点“关闭”,它只是返回连接到它的节点。在这个例子中它应该返回label1和label2,但不是label3。Neo4j检查属性密码
答
您可以使用optional match
和with
组合为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
答
我假设 '开' 和 '关' 是对参数的状态。我将在代码中将其称为状态。 我还假设你想从一个特定的节点开始。 (a) - [r:connect * ..] - >(b)其中ID(n)= 1我不确定你是如何得到这个节点的,所以我会假设你有这个ID并且它等于1
MATCH b.State ='on'return b
如果'label3'连接到另一个打开的绿色节点会发生什么? – cybersam
它应该仍然返回相同的。在这种情况下,我们从label1开始,如果我们要从label3开始,那么它会有所不同。 –