【发布时间】:2018-12-27 20:28:11
【问题描述】:
我正在编写以下 Neo4j Cypher 查询以添加新的 Part 并从 Vehicle 节点中删除旧的 Part。我正在使用 Neo4j(版本 3.4.1)
newParts 和 removedParts 是我的参数。它们是String 的列表。
with {newParts} AS newParts
unwind newParts as newPart
match (r:Vehicle) where id(r)=639
merge (r)-[:HAS_PART]->(np:Part{name:newPart})
on create np+={lastModifiedDateTime:localdatetime(), createdDateTime:localdatetime()}
with r optional match (r)-[rel:HAS_PART]-(p:Part) where p.name in {removedParts}
delete rel
with r match q=(r)--()
return nodes(q), relationships(q))
当我提供 newParts 参数为非空时它工作正常。
但是,当它为空时,我无法取回最终的节点和关系。我确实理解为什么会发生这种情况,因为当列表为空时 unwind 会停止执行。我尝试将with..unwind 部分移动到del 下方,它成功删除了removedParts Part。
但是,它不会在展开后返回最终节点和关系。
我也不确定如何使用空的 newParts 参数来完成这项工作。我试图使用case,但我认为case 不适用于节点和关系。
任何帮助或指点将不胜感激。
提前致谢
V
【问题讨论】:
-
您是否尝试过使用
case size(newParts) ...?
标签: neo4j cypher spring-data-neo4j