CouchDB:使用 Reduce 时如何在 Map 函数中使用数组键?

CouchDB: How to use array keys in Map functions when using Reduce?

我想在 CouchDB 中编写一个 MapReduce 函数,其中 Map 函数将键作为数组发出,但 reduce 函数仅使用映射键中的一个值。例如:

地图函数:

function(doc) {
    if (doc.type_ === 'survey') {
        emit([doc.timeRecorded_, doc.imei_], 1);
    };
};

Reduce 函数:

function(k,v) {

  // How to handle only the doc.imei_ as the value?
  // Or, alternatively, how to filter based on timeRecorded_ somewhere other than the map function?
  return sum(v)
}

timeRecorded_ 在一个 EPOCH 编号中,所以不会有重复(除非偶然)。如果我要对其进行聚合,则需要将其四舍五入为 'day' 值。或者,可以以 timeRecorded_ 已经在源数据中四舍五入的方式准备数据(可能更改为 dateRecorded_)

此问题的一个众所周知的模式是将日期拆分为一个数组(例如 [year, month, day, hour, minute];间隔可以不同,但​​要保持顺序)并使用该数组作为地图功能。

因此,您可以根据需要 group_level 减少行数(例如 "by year"、"by month"、"by day"、"by hour"、 "by minute",等等)。

来源:http://blog.couchbase.com/understanding-grouplevel-view-queries-compound-keys