我有类型为Challenge,Entry,User,Comment
的节点我尝试查询作为PART_OF的所有条目,这些条目已被LIKED或由特定用户评论,或者具有由同一用户发布的条目(这些条件中的一个或多个)。
MATCH (u:User {id: 'r1tcX0vxW'})-[:LIKES]->(c:Challenge)
WITH COLLECT (c) as likedChallenges
MATCH (c:Challenge)<-[:POSTED_IN]-(comment:Comment)-[:POSTED_BY]->(u)
WITH likedChallenges, COLLECT (c) as commentedChallenges
MATCH (c:Challenge)<-[:PART_OF]-(e:Entry)-[:POSTED_BY]->(u)
WITH likedChallenges + commentedChallenges + COLLECT (c) AS allChallenges
UNWIND allChallenges as c
MATCH (e:Entry)-[:PART_OF]->(c)
RETURN e;
我使用COLLECT子句来完成这项工作,但问题是Challenge节点有重复的输出,我不知道如何删除重复项。
答案 0 :(得分:4)
您可以在最后一个句子中使用DISTINCT
来返回不同的条目:
RETURN DISTINCT e;
注意:您的Cypher查询中存在大量语法错误。这应该更好:
MATCH (u:User {id: 'r1tcX0vxW'})-[:LIKES]->(c:Challenge)
WITH u, COLLECT(c) as all
MATCH (c:Challenge)<-[:POSTED_IN]-(:Comment)-[:POSTED_BY]->(u)
WITH u, all + COLLECT(c) AS all
MATCH (c:Challenge)<-[:PART_OF]-(:Entry)-[:POSTED_BY]->(u)
WITH all + COLLECT(c) AS all
UNWIND all AS c
MATCH (e:Entry)-[:PART_OF]->(c)
RETURN DISTINCT e;