如何在条件创建后让 Neo4J 继续
How do I make Neo4J continue after conditional create
创建节点集合后,某些节点还应具有基于条件的附加关系。在下面的例子中,条件是用 WHERE n.number > 3
模拟的,节点是简单的数字:
WITH [2, 3, 4] as numbers
UNWIND numbers AS num
CREATE(n:Number {number: num})
WITH collect(n) AS nodes
UNWIND nodes AS n
WITH nodes, n WHERE n.number > 3
CREATE (n)-[:IM_SPECIAL]->(n)
RETURN nodes
哪个returns:
╒════════════════════════════════════════╕
│"nodes" │
╞════════════════════════════════════════╡
│[{"number":2},{"number":3},{"number":4}]│
└────────────────────────────────────────┘
Added 3 labels, created 3 nodes, set 3 properties, created 1 relationship, started streaming 1 records in less than 1 ms and completed after 1 ms.
我的问题是,除非我至少有一个被过滤器捕获的“特殊”节点,否则不会返回任何内容。可以通过将输入数字更改为 [1, 2, 3]
来模拟该问题,其中 returns 一个空结果(没有节点),即使创建了节点(因为它们应该):
<empty result>
Added 3 labels, created 3 nodes, set 3 properties, completed after 2 ms.
我可能完全错误地解决了这个问题,但我已经用尽了我的 Google 技能......我缺少什么 Neo4J Cypher 魔法?
WITH nodes, n WHERE n.number > 3
Cypher 查询的每个子句都必须产生一个结果,供查询的后续行使用。如果您以 [1,2,3].
开头,则以上行不会产生任何结果
为了您的目的,这会起作用。
WITH [1,2,3,4] as numbers
UNWIND numbers AS num
CREATE(n:Number {number: num})
WITH n
CALL apoc.do.when(n.number>3,
'CREATE (n)-[:IM_SPECIAL]->(n) RETURN n',
'RETURN n',
{n:n}
)
YIELD value as m
WITH collect(m) AS nodes
RETURN nodes
关于 Conditional Cypher Execution - Using correlated subqueries in 4.1+ 的文档描述了如何在不需要 Apoc 的情况下解决这个问题:
WITH [2, 3, 4] AS numbers
UNWIND numbers AS num
CREATE(n:Number {number: num})
WITH n
CALL {
WITH n
WITH n WHERE n.number > 3
CREATE (n)-[:IM_SPECIAL]->(n)
RETURN count(n)
}
RETURN collect(n) AS nodes
感谢 Sanjay Singh 和 Jose Bacoy 让我走上正轨。
创建节点集合后,某些节点还应具有基于条件的附加关系。在下面的例子中,条件是用 WHERE n.number > 3
模拟的,节点是简单的数字:
WITH [2, 3, 4] as numbers
UNWIND numbers AS num
CREATE(n:Number {number: num})
WITH collect(n) AS nodes
UNWIND nodes AS n
WITH nodes, n WHERE n.number > 3
CREATE (n)-[:IM_SPECIAL]->(n)
RETURN nodes
哪个returns:
╒════════════════════════════════════════╕
│"nodes" │
╞════════════════════════════════════════╡
│[{"number":2},{"number":3},{"number":4}]│
└────────────────────────────────────────┘
Added 3 labels, created 3 nodes, set 3 properties, created 1 relationship, started streaming 1 records in less than 1 ms and completed after 1 ms.
我的问题是,除非我至少有一个被过滤器捕获的“特殊”节点,否则不会返回任何内容。可以通过将输入数字更改为 [1, 2, 3]
来模拟该问题,其中 returns 一个空结果(没有节点),即使创建了节点(因为它们应该):
<empty result>
Added 3 labels, created 3 nodes, set 3 properties, completed after 2 ms.
我可能完全错误地解决了这个问题,但我已经用尽了我的 Google 技能......我缺少什么 Neo4J Cypher 魔法?
WITH nodes, n WHERE n.number > 3
Cypher 查询的每个子句都必须产生一个结果,供查询的后续行使用。如果您以 [1,2,3].
开头,则以上行不会产生任何结果为了您的目的,这会起作用。
WITH [1,2,3,4] as numbers
UNWIND numbers AS num
CREATE(n:Number {number: num})
WITH n
CALL apoc.do.when(n.number>3,
'CREATE (n)-[:IM_SPECIAL]->(n) RETURN n',
'RETURN n',
{n:n}
)
YIELD value as m
WITH collect(m) AS nodes
RETURN nodes
关于 Conditional Cypher Execution - Using correlated subqueries in 4.1+ 的文档描述了如何在不需要 Apoc 的情况下解决这个问题:
WITH [2, 3, 4] AS numbers
UNWIND numbers AS num
CREATE(n:Number {number: num})
WITH n
CALL {
WITH n
WITH n WHERE n.number > 3
CREATE (n)-[:IM_SPECIAL]->(n)
RETURN count(n)
}
RETURN collect(n) AS nodes
感谢 Sanjay Singh 和 Jose Bacoy 让我走上正轨。