Neo4j如何从一组节点ID创建/删除关系

时间:2016-03-09 07:34:25

标签: neo4j cypher

我有一个User个节点和Intersets个节点。我希望能够给出一系列兴趣来创建/删除/更改UserInterests之间的关系,我也希望在同一个查询中更新用户节点上的某些属性。

到目前为止,这是我要做的事情:

MATCH (user:User {id: id})
OPTIONAL MATCH (user)-[oldRel:InterestedIn]->(:Interest)
DETACH DELETE oldRel
WITH user
UNWIND {interestsIds} as id
MATCH (interest:Interest {id: id})
MERGE (user)-[rel: InterestedIn]->(interest)
SET user.name = {user}.name, ..(more sets)
RETURN user, collect(interest) as interests

我认为这个工作有时候看起来像interests被重复复制了.. 同样,这个查询看起来有点矫枉过正。知道如何以更好的方式进行查询吗?

1 个答案:

答案 0 :(得分:2)

这看起来是对的吗?

MATCH (user:User {id: id})
OPTIONAL MATCH (interest:Interest)
WHERE interest.id IN {interestsIds}
MERGE (user)-[:InterestedIn]->(interest)

WITH DISTINCT user
MATCH (user)-[rel:InterestedIn]->(interest:Interest)
WHERE NOT(interest.id IN {interestsIds})
DELETE rel

WITH DISTINCT user
MATCH (user)-[:InterestedIn]->(interest:Interest)
RETURN user, collect(interest)