Couchbase N1QL - 尝试在 Couchbase 查询编辑器中执行两个文档之间的连接操作但没有得到任何结果

Couchbase N1QL - Trying to perform join operation between two documents in couchbase query editor but not getting any result

Couchbase - N1QL 查询 我正在尝试将两个文档加入同一个存储桶中,并且必须执行 GroupBy“DepartmentName”并依靠其“TerminalCheckinStatus”为“COMPLETED”的“TeamMemberID”

**First Type of Document**
docstore {
   DepartmentName: "Medical",
   TeamMemberID: "ABC123",
   type: "TeamMember"
}

**Second Type of Document**
docstore {
   TerminalCheckinStatus: "COMPLETED",
   TeamMemberID: "ABC123",
   type: "TeamMemberStatus"
}

首先我尝试对两个文档进行“JOIN”操作

select a.TeamMemberID, a.DepartmentName
FROM `docstore` a
JOIN `docstore` b
ON a.TeamMemberID = META(b).id;

但没有得到任何结果。两个文档的公共字段中都有“TeamMemberID”

过去两天我一直在为此苦苦挣扎,非常感谢这方面的任何帮助。提前致谢:)

CREATE INDEX ix1 ON `docstore` (TeamMemberID, DepartmentName) WHERE type = "TeamMember";
CREATE INDEX ix2 ON `docstore` (TeamMemberID, TerminalCheckinStatus) WHERE type = "TeamMemberStatus";

SELECT COUNT(b.TeamMemberID) AS count, a.DepartmentName
FROM `docstore` AS a
LEFT JOIN `docstore` AS b 
      ON a.TeamMemberID = b.TeamMemberID 
         AND b.type = "TeamMemberStatus" 
         AND b.TerminalCheckinStatus = "COMPLETED"
WHERE a.type = "TeamMember" AND a.TeamMemberID IS NOT NULL
GROUP BY a.DepartmentName;

https://blog.couchbase.com/ansi-join-support-n1ql/

或者,我们也可以将@vsr answer 写成 带有子查询

SELECT COUNT(*) AS count, a.DepartmentName
FROM docstore a
WHERE a.TeamMemberID in
   (SELECT raw b.TeamMemberID from docstore b
   where b.type = "TeamMemberStatus" 
   and b.TerminalCheckinStatus = "COMPLETED" 
   and meta(b).id not like "_sync%")
and meta(a).id not like "_sync%"
Group by a.DepartmentName ;