CouchDBのmap/reduce関数でこれを行うことができます。
のは、あなたがそれらの文書を持っているとしましょう:
{
"_id": "report_1",
"type": "report",
"block_id": "block_1",
"date": "1500325245"
}
{
"_id": "report_2",
"type": "report",
"block_id": "block_1",
"date": "1153170045"
}
あなたは(この場合は、repot_1)最高のタイムスタンプを使用してレポートを取得したいと思います。
まず、bloc_idをキーとし、タイムスタンプ+レポートIDをreduce関数の値としてマップするmap関数を作成します。
地図:
function (doc) {
if(doc.type == "report")
emit(doc.block_id,{date:doc.created,report:doc._id});
}
その後、我々はreduce関数を作成します。 rereduceがfalseの場合、値を返すだけです。 rereduceがtrueの場合、我々は
削減、それに関連するレポートIDを最大タイムスタンプを見つけて返す関数:
function (keys, values, rereduce) {
if (rereduce) {
var max = 0;
var maxReportId = -1;
for (var i = 0; i < values.length; i++) {
var val = values[i][0];
if (parseInt(val.date) > max) {
max = val.date;
maxReportId = val.report;
}
}
//We return the report id of the most recent report.
return maxReportId;
} else
return values;
}