【发布时间】:2018-06-13 07:49:12
【问题描述】:
我有一个相当大的 MongoDB,我需要从中提取统计信息,然后我购买了运行 Map Reduce 查询。
现在的问题是我需要缩小查询范围以使用例如 status: 'drafted" 而不是使用整个集合。
这是我的 Map/Reduce 代码(我正在使用 Codeigniter): 我尝试执行此查询的最后一步,但无法获得结果,因此我认为我添加了错误的语法:http://cookbook.mongodb.org/patterns/unique_items_map_reduce/。
$map = new MongoCode ("function() {
day = Date.UTC(this.created_at.getFullYear(), this.created_at.getMonth(), this.created_at.getDate());
emit ({day: day, _id: this._id}, {created_at: this.created_at, count: 1});
}");
$reduce = new MongoCode ("function( key , values ) {
var count = 0;
values.forEach (function(v) {
count += v['count'];
});
return {count: count};
}");
$outer = $this->cimongo->command (array (
"mapreduce" => "documents",
"map" => $map,
"reduce" => $reduce,
"out" => "stats_results"
));
$map = new MongoCode ("function() {
emit(this['_id']['day'], {count: 1});
}");
$reduce = new MongoCode ("function( key , values ) {
var count = 0;
values.forEach (function(v) {
count += v['count'];
});
return {count: count};
}");
$outer = $this->cimongo->command (array (
"mapreduce" => "stats_results",
"map" => $map,
"reduce" => $reduce,
"out" => "stats_results_unique"
));
【问题讨论】:
-
“使用状态‘草稿’”是什么意思?你想避免映射整个表,还是只发出具有该状态的键就足够了?我猜你想要的是根据状态字段有条件地发出结果。