在Cypher查询中筛选掉可选的匹配或结果。

huangapple go评论54阅读模式
英文:

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的关系丢失了。我想要从结果中同时移除UserTeam

英文:

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;

huangapple
  • 本文由 发表于 2023年6月1日 14:40:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76379275.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定