一系列不执行 neo4j 密码的条件 foreach



我正在尝试将FOREACH用于条件语句。我正在传递一个参数$hasLegs如果值等于"Y">,我执行合并。但是,这似乎不想执行,因为整个操作被绕过并且不会运行。

我已经尝试了FOREACH方法,使用APOC并使用WITH,但到目前为止没有运气。也许这不是我的一周。

对于每个示例:

MERGE (species:Species {name: $name})
ON CREATE SET species.GUID = apoc.create.uuid(), species.creationDate = datetime()
WITH species
with species, $hasLegs as hasLegs
FOREACH (ignore IN CASE WHEN $hasLegs = 'Y' THEN [1] ELSE [] END | MERGE (species)-[:LINKS_TO]->(:Question:Root{type: 'Legs'}) )
RETURN species

APOC 示例

MERGE (species:Species {name: $name})
ON CREATE SET species.GUID = apoc.create.uuid(), species.creationDate = datetime()
WITH species
MATCH (l:Question:Root{type: 'Legs')
CALL apoc.when(hasLegs='Y', 'MERGE (species)-[:LINKS_TO]->(l)','', { l:l, species: species }) YIELD value
WITH value AS ignored, species
RETURN species

预期的输出只是,参数为:$name$hasLegs

$name用于防止重复节点的 MERGE,但$hasLegs等于"Y">null

(物种(-[:LINK_TO]->(问题(

任何帮助将不胜感激! 谢谢!

您的查询似乎工作正常,每个查询的使用都是正确的。

但是,每次执行时,它都会创建新的Question节点。要使用唯一的问题节点,请使用 2 个MERGE子句:

MERGE (q:Question:Root{type: 'Legs'}) MERGE (species)-[:LINKS_TO]->(q)

整个查询:

MERGE (species:Species {name: $name})
ON CREATE SET species.GUID = apoc.create.uuid(), species.creationDate = datetime()
WITH species
with species, $hasLegs as hasLegs
FOREACH (ignore IN CASE WHEN $hasLegs = 'Y' THEN [1] ELSE [] END | MERGE (q:Question:Root{type: 'Legs'}) MERGE (species)-[:LINKS_TO]->(q) )
RETURN species

最新更新