我正在将Neo4j 3.5.2 Desktop与Nodejs一起使用。我正在尝试更新用户记录属性,并在同一查询中添加/删除与其他节点的关系: 我的查询如下:
MATCH (user:Dealer {email: $paramObj.email})
SET user += apoc.map.clean($paramObj, ["email","vehicles"],[])
WITH user, $paramObj.vehicles AS vehicles
UNWIND vehicles AS vehicle
MATCH(v:Vehicles {name:vehicle})
MERGE (user)-[r:SUPPLY_PARTS_FOR]->(v)
ON CREATE SET r.since = timestamp()
WITH vehicles,user
MATCH (user)-[r:SUPPLY_PARTS_FOR]->(v)
WHERE NOT apoc.coll.contains(vehicles,v.name)
DELETE r
WITH $paramObj.email AS dealeremail
MATCH (user:Dealer {email: dealeremail})
RETURN user
我遇到的问题是,当与删除车辆关系(r)有关的查询导致零行时,返回空的“用户”数组。
如何保留原始的“用户”结果或保存电子邮件地址以重做查询。我尝试将WITH $ paramObj.email作为DealerEmail使用,但似乎无法转发DealerEmail ...可以。
答案 0 :(得分:0)
此问题是由于返回零行导致的,所以我想到OPTIONAL MATCH也将返回NULL结果,但只有一行具有空值。因此,我更改了“匹配”以搜索要删除的关系,并将其更改为“可选匹配”。
MATCH (user:Dealer {email: $paramObj.email})
SET user += apoc.map.clean($paramObj, ["email","vehicles"],[])
WITH user, $paramObj.vehicles AS vehicles
UNWIND vehicles AS vehicle
MATCH(v:Vehicles {name:vehicle})
MERGE (user)-[r:SUPPLY_PARTS_FOR]->(v)
ON CREATE SET r.since = timestamp()
WITH vehicles,user
OPTIONAL MATCH (user)-[r:SUPPLY_PARTS_FOR]->(v)
WHERE NOT apoc.coll.contains(vehicles,v.name)
DELETE r
RETURN user
这成功了