英文:
Filtering out optional matches or results in cypher query
问题
我有一个User
电子邮件列表,我需要获取所有这些用户及其Team
成员资格。包括那些没有加入任何Team
的用户。我有一个查询如下:
MATCH (u:User)
WHERE (u.email in $userEmails)
OPTIONAL MATCH (u)-[r:MEMBER_OF]-(t:Team)
RETURN r, u, t;
我遇到的问题是,我还想在查询中排除一些Teams
。当我尝试这样做时:
MATCH (u:User)
WHERE (u.email in $userEmails)
OPTIONAL MATCH (u)-[r:MEMBER_OF]-(t:Team)
WHERE NOT (t.id in $excludedTeamIds)
RETURN r, u, t;
结果是相同的Users
列表,但是它们与Team
的关系丢失了。我想要从结果中同时移除User
和Team
。
英文:
I have a list of User
emails and I need to get all of them with their Team
membership. Both Users
with Teams
and those who are not members of any Team
. I have a query for that which is:
MATCH (u:User)
WHERE (u.email in $userEmails)
OPTIONAL MATCH (u)-[r:MEMBER_OF]-(t:Team)
RETURN r, u, t;
The problem I have is that I would also want to exclude a list of Teams
in my query. When I tried this:
MATCH (u:User)
WHERE (u.email in $userEmails)
OPTIONAL MATCH (u)-[r:MEMBER_OF]-(t:Team)
WHERE NOT (t.id in $excludedTeamIds)
RETURN r, u, t;
The result was the same list of Users
, but their relation
to the Team
was just missing. I would want to remove both - the User
and the Team
from the result.
答案1
得分: 1
你应该使用额外的 WITH
子句来筛选用户和团队,就像这样:
MATCH (u:用户)
WHERE (u.email IN $userEmails)
OPTIONAL MATCH (u)-[r:是成员]-(t:团队)
WITH u, r, t WHERE t IS NULL OR NOT (t.id IN $excludedTeamIds)
RETURN r, u, t;
英文:
You should filter the users and teams, using an additional WITH
clause. Like this:
MATCH (u:User)
WHERE (u.email in $userEmails)
OPTIONAL MATCH (u)-[r:MEMBER_OF]-(t:Team)
WITH u, r, t WHERE t IS NULL OR NOT (t.id IN $excludedTeamIds)
RETURN r, u, t;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论