我创建了一个层次结构树来表示Neo4j上公司的组织结构图,如下图所示。
当我与LOAD CSV
插入很多关系时,我会使用以下请求:
LOAD CSV WITH HEADERS FROM "file:///newRelation.csv" AS row
MERGE (a:Person {name:row.person1Name})
MERGE(b:Person {name:row.person2Name})
FOREACH (t in CASE WHEN NOT EXISTS((a)-[*]->(b)) THEN [1] ELSE [] END |
MERGE (a)-[pr:Manage]->(b) )
对于此请求,我仅在两个人之间尚未建立层次关系的情况下创建关系。
如何保存(记录)由于以下测试失败而未创建的关系列表?
CASE WHEN NOT EXISTS((a)-[*]->(b)
答案 0 :(得分:1)
您需要将存在性检查移至foreach之上的级别:
LOAD CSV WITH HEADERS FROM "file:///newRelation.csv" AS row
MERGE (a:Person {name:row.person1Name})
MERGE(b:Person {name:row.person2Name})
WITH a, b, row,
CASE WHEN NOT exists((a)-[*]->(b)) THEN [1] ELSE [] END AS check
FOREACH (t IN check |
MERGE (a)-[pr:Manage]->(b)
)
WITH a, b, row, check WHERE size(check) = 0
RETURN a, b, row