这是本教程的代码示例:
http://kylebanker.com/blog/2009/12/mongodb-map-reduce-basics/
他指出“从MongoDB v1.8开始,您必须指定输出集合名称。”但是我没有看到这被提到的地方或为什么需要它。
# Running map-reduce from Ruby (irb) assuming
# that @comments references the comments collection
# Specify the map and reduce functions in JavaScript, as strings
>> map = "function() { emit(this.author, {votes: this.votes}); }"
>> reduce = "function(key, values) { " +
"var sum = 0; " +
"values.forEach(function(doc) { " +
" sum += doc.votes; " +
"}); " +
"return {votes: sum}; " +
"};"
# Pass those to the map_reduce helper method
@results = @comments.map_reduce(map, reduce, :out => "mr_results")
# Since this method returns an instantiated results collection,
# we just have to query that collection and iterate over the cursor.
>> @results.find().to_a
=> [{"_id" => "hwaet", "value"=>{"votes"=>21.0}},
{"_id" => "kbanker", "value"=>{"votes"=>13.0}}
]
答案 0 :(得分:1)
新的Map / Reduce输出选项记录在here。
基本前提是Map / Reduce最初只输出到临时集合。临时集合中存在一些问题,(为什么所有这些都只是暂时的?)并且在合并和重新缩减时添加了一些功能。
特别是,您现在可以运行一个有效更新先前M / R输出的M / R. (想想每小时更新一次每日统计数据,只处理最后一小时)。
但是,如果您只想要结果的内存版本,则可以使用内联选项。
{out: { inline : 1}}