我有一个相当大的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"
));
答案 0 :(得分:12)
关于你的问题的两件事:
1)食谱中的例子可能对你想做的事情有点过于复杂。这是一个更简单的一个:
给定一个如下所示的文档结构:
{
"url" : "http://example.com/page8580.html",
"user_id" : "Jonathan.Clark",
"date" : ISODate("2012-06-11T10:59:36.271Z")
}
以下是一些示例JavaScript代码,用于运行map / reduce作业,该作业将计算每个不同网址的访问次数。
// Map function:
map = function() {
emit({ url: this.url }, {count: 1});
}
// Reduce function:
reduce = function(key, values) {
var count = 0;
values.forEach(
function(val) { count += val['count']; }
);
return {count: count};
};
// Run the Map/Reduce function across the 'pageviews' collection:
// Note that MongoDB will store the results in the 'pages_per_day'
// collection because the 'out' parameter is present
db.pageviews.mapReduce(
map, // pass in the 'map' function as an argument
reduce, // pass in the 'reduce' function as an argument
// options
{ out: 'pages_per_day', // output collection
verbose: true } // report extra statistics
);
2)如果你只想在'pageviews'集合的一个子集上运行Map / Reduce函数,你可以指定对'mapReduce()'调用的查询,以限制文件的数量。 'map()'函数将在以下位置运行:
// Run the Map/Reduce function across the 'pageviews' collection, but
// only report on the pages seen by "Jonathan.Clark"
db.pageviews.mapReduce(
map, // Use the same map & reduce functions as before
reduce,
{ out: 'pages_per_day_1user', // output to different collection
query:{ 'user_id': "Jonathan.Clark" } // query descriptor
verbose: true }
);
请注意,如果您不使用JavaScript,则必须将这些调用转换为您正在使用的任何编程语言。
3)以下是使用PHP调用Map / Reduce函数和查询条件的示例:
$outer = $this->cimongo->command (array (
"mapreduce" => "pageviews",
"map" => $map,
"reduce" => $reduce,
"out" => "pages_per_day_1user",
"query" => array( "user_id" => "Jonathan.Clark" )
));
4)有关Map / Reduce的更多信息,请参阅以下参考:
在PHP中使用Map / Reduce:http://us.php.net/manual/en/mongodb.command.php
- 威廉