除了3个单独的查询之外,还有更好的方法吗?
MATCH (you:User {name: "Alexander"})-[:LIKES]->(youLike:User)
RETURN youLike
MATCH (likesYou:User)-[:LIKES]->(you:User {name: "Alexander"})
RETURN likesYou
MATCH (mutualLike:User)-[:LIKES]->(you:User {name: "Alexander"})-[:LIKES]->(mutualLike:User)
RETURN mutualLike
答案 0 :(得分:3)
以下是单个查询的镜头。
基本上,找到自己的第一个,可选择找到你喜欢的人并收集它们,可选择找到喜欢你的人并收集它们,然后返回两个集合和两者的交集。
通过匹配标识您并重新使用它的节点,您可以匹配一次而不是三次。
使用集合过滤器功能,您可以找到两个:LIKES
种群的交集,而无需重新匹配这些节点。
如果OPTIONAL
填充为空,则:LIKES
关键字允许查询继续。
MATCH (you:User {name: "Alexander"})
WITH you
OPTIONAL MATCH(you)-[:LIKES]->(youLike:User)
WITH you, collect(youLike) as youLike
OPTIONAL MATCH (likesYou:User)-[:LIKES]->(you)
WITH you, youLike, collect(likesYou) as likesYou
RETURN you
, youLike
, likesYou
, filter(n in youLike where n in likesYou) as mutualLike