如何使Neo4J在条件创建后继续

tf7tbtn2  于 2022-11-05  发布在  其他
关注(0)|答案(2)|浏览(154)

建立节点集合之后,某些节点也应该根据条件附加关系。在下列范例中,条件是以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

它会传回:

╒════════════════════════════════════════╕
│"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]来模拟,即使节点已经创建(应该创建),它也会返回一个空结果(没有节点):

<empty result>
Added 3 labels, created 3 nodes, set 3 properties, completed after 2 ms.

我可能是完全错误的处理问题,但我已经用尽了我的谷歌技能...我错过了什么Neo4J Cypher魔术?

jtw3ybtb

jtw3ybtb1#

关于条件密码执行-在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

感谢桑杰·辛格和何塞·巴科伊让我走上了正确的道路。

mbyulnm0

mbyulnm02#

WITH节点,n,其中n.编号〉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

相关问题