> db.collection_name.mapReduce( function() {emit(key, value);}, // map 函数 function(key, values) {return reduceFunction}, // reduce 函数 { out: collection, query: document, sort: document, limit: number } )参数说明如下:
{
"post_text": "编程帮(lmcjl.com),一个在线学习编程的网站,专注于分享优质编程教程。",
"user_name": "bianchengbang",
"status":"active"
}
"status":"active"
的文档,然后根据用户名对它们进行分组,最后统计每个用户的发帖数量。示例代码如下:> db.posts.mapReduce( ... function() { emit(this.user_name,1); }, ... function(key, values) {return Array.sum(values)}, ... { ... query:{status:"active"}, ... out:"post_total" ... } ... )上面 mapReduce 命令的输出结果如下所示:
{ "result" : "post_total", "timeMillis" : 48, "counts" : { "input" : 11, "emit" : 11, "reduce" : 2, "output" : 2 }, "ok" : 1 }关于运行结果,有如下几点需要说明:
> db.posts.mapReduce( ... function() { emit(this.user_name,1); }, ... function(key, values) {return Array.sum(values)}, ... { ... query:{status:"active"}, ... out:"post_total" ... } ... ).find()上面的查询语句,执行结果如下:
{ "_id" : "biancheng", "value" : 5 }
{ "_id" : "bianchengbang", "value" : 6 }
本文链接:http://task.lmcjl.com/news/16744.html