我有一系列文件,每个文件都包含一个日期和一个字母列表。我需要输出为每个键的单个唯一字母,每个字母的最新日期为值。
使用简单的映射函数,如下所示:
{
"_id": "_design/output",
"_rev": "1-c7b904b0e93709113c49127efb40187a",
"language": "javascript",
"views": {
"output": {
"map": "function(doc) {\n emit(doc.args.report.updated_partitions, doc.timestamp);\n}",
"reduce": "function(key, value, reduce) {\n return (value)\n}"
}
}
}
我得到以下结果:
{"total_rows":2,"offset":0,"rows":[
{"id":"8e561dd15fd33acfc7c4502c58008c2a","key":["a","b"],"value":1403818824},
{"id":"8e561dd15fd33acfc7c4502c5800a212","key":["a","c","d"],"value":1403818700}
]}
我想要的输出应该是:
{"total_rows":2,"offset":0,"rows":[
{"id":"8e561dd15fd33acfc7c4502c58008c2a","key":["a"],"value":1403818824},
{"id":"8e561dd15fd33acfc7c4502c58008c2a","key":["b"],"value":1403818824},
{"id":"8e561dd15fd33acfc7c4502c58008c2a","key":["c"],"value":1403818824},
{"id":"8e561dd15fd33acfc7c4502c5800a212","key":["d"],"value":1403818700}
]}
答案 0 :(得分:1)
我实际上找到了一种更简单的方法。基本步骤是:
地图如下:
function(doc) {
var files = doc.args.report.updated_partitions;
var timestamp = doc.timestamp;
for (i=0; i<files.length; i+= 1){
var file_name = files[i];
emit(file_name, timestamp);
}
}
缩小看起来像:
function(key, value, reduce) {
return (Math.max.apply(null, value))
}
结果将是每个字母的唯一键,其中所有文档的最新日期为值。
这个用例是每个沙发文档都有一个修改过的文件列表和修改日期,但是您想知道每个文档的最新修改日期。