我有一个User
个节点和Intersets
个节点。我希望能够给出一系列兴趣来创建/删除/更改User
和Interests
之间的关系,我也希望在同一个查询中更新用户节点上的某些属性。
到目前为止,这是我要做的事情:
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
被重复复制了..
同样,这个查询看起来有点矫枉过正。知道如何以更好的方式进行查询吗?
答案 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)