在 foreach 中合并和删除 case 语句
Merge and delete case statement inside foreach
如果月份节点内的属性在字符串数组 ($monthsArray) 中,查询将尝试合并关系。字符串数组在 cypher 中作为参数传入。如果匹配则将创建关系,但是如果不匹配则应删除两个节点之间的关系。
我知道 case 语句不支持 MATCH 和 MERGE 子句,但是我将它们放在下面以帮助解释我想要实现的目标。
谢谢!
MERGE (species:Reference:Species{ GUID: $reference.guid })
MATCH (months:Month) // MATCH ALL MONTHS NODES
FOREACH (month IN months)
CASE WHEN month.name IN $monthsArray // Where $monthsArray is a parameterised string array [ "January", "March"... ]
THEN
MERGE (species)-[:MEASURED_BY]->(month)
ELSE
DETACH DELETE relationships((species)-[:MEASURED_BY]->(month))
如果您将月份的 MATCH
es 分成两组(需要添加的和需要删除的),那么您可以盲目地 FOREACH
处理这两组:
MERGE (species: Species { GUID: $reference.guid })
OPTIONAL MATCH (toAdd: Month) WHERE toAdd.name IN $monthsArray AND NOT ((species)-[:MEASURED_BY]->(toAdd))
WITH collect(toAdd) as toAddMonths, species
OPTIONAL MATCH (species)-[toRemove:MEASURED_BY]->(toRemoveMonth: Month) WHERE NOT toRemoveMonth.name IN $monthsArray
WITH collect(toRemove) as toRemoveRels, toAddMonths, species
FOREACH (toAdd in toAddMonths | MERGE (species)-[:MEASURED_BY]->(toAdd))
FOREACH (toRemoveRel in toRemoveRels | DELETE toRemoveRel)
如果月份节点内的属性在字符串数组 ($monthsArray) 中,查询将尝试合并关系。字符串数组在 cypher 中作为参数传入。如果匹配则将创建关系,但是如果不匹配则应删除两个节点之间的关系。
我知道 case 语句不支持 MATCH 和 MERGE 子句,但是我将它们放在下面以帮助解释我想要实现的目标。
谢谢!
MERGE (species:Reference:Species{ GUID: $reference.guid })
MATCH (months:Month) // MATCH ALL MONTHS NODES
FOREACH (month IN months)
CASE WHEN month.name IN $monthsArray // Where $monthsArray is a parameterised string array [ "January", "March"... ]
THEN
MERGE (species)-[:MEASURED_BY]->(month)
ELSE
DETACH DELETE relationships((species)-[:MEASURED_BY]->(month))
如果您将月份的 MATCH
es 分成两组(需要添加的和需要删除的),那么您可以盲目地 FOREACH
处理这两组:
MERGE (species: Species { GUID: $reference.guid })
OPTIONAL MATCH (toAdd: Month) WHERE toAdd.name IN $monthsArray AND NOT ((species)-[:MEASURED_BY]->(toAdd))
WITH collect(toAdd) as toAddMonths, species
OPTIONAL MATCH (species)-[toRemove:MEASURED_BY]->(toRemoveMonth: Month) WHERE NOT toRemoveMonth.name IN $monthsArray
WITH collect(toRemove) as toRemoveRels, toAddMonths, species
FOREACH (toAdd in toAddMonths | MERGE (species)-[:MEASURED_BY]->(toAdd))
FOREACH (toRemoveRel in toRemoveRels | DELETE toRemoveRel)