如何在 couchbase 中为此 sql 语句编写视图

How to write a view in couchbase for this sql statement

假设我有以下文件

Document 1
{
  companyId: "1",
  salesDate: "1425254400000" //this is UTC time as a long
}
Document 2
{
  companyId: "1",
  salesDate: "1425340800000" //this is UTC time as a long
}
Document 3
{
  companyId: "2",
  salesDate: "1425254400000" //this is UTC time as a long
}

我目前的视图设置为

function(doc, meta) { emit([doc.salesDate, doc.companyId], doc); }

使用

时会撤回所有 3 个文档
?startkey=[1425254400000,"1"]&endkey=[1425340800000,"1"]

我不确定如何让它只按公司 ID 拉回该日期范围内的销售额。

sql 版本将是 SELECT * FROM sales WHERE companyId = :companyId AND salesDate BETWEEN :rangeStart AND :rangeEnd

编辑:我正在使用其余的 API。

在为多查询字段的范围查询设计视图时,固定查询字段(companyId)应作为复合索引的前缀,范围查询字段应在末尾。在当前视图下,Couchbase 将发出 salesDate 在范围内的每个文档,而不考虑 companyId.

颠倒按键顺序即可:

function(doc, meta) { 
  emit([doc.companyId, doc.salesDate], doc); 
}

查询:

?startkey=["1", 1425254400000]&endkey=["1", 1425340800000]

N.B。如果 salesDate 是字符串而不是数值,Couchbase 将使用字典顺序来执行范围查询。