mongodb的mapreduce使用

先插入数据,插入的数据如下

> db.books.find()
{ "_id" : ObjectId("533ee1e8634249165a819cd0"), "name" : "apue", "pagenum" : 1023 }
{ "_id" : ObjectId("533ee273634249165a819cd1"), "name" : "clrs", "pagenum" : 2000 }
{ "_id" : ObjectId("533ee2ab634249165a819cd2"), "name" : "python book", "pagenum" : 600 }
{ "_id" : ObjectId("533ee2b7634249165a819cd3"), "name" : "golang book", "pagenum" : 400 }
{ "_id" : ObjectId("533ee2ca634249165a819cd4"), "name" : "linux book", "pagenum" : 1500 }


写map函数

var map = function() {
    var category;
    if ( this.pagenum >= 1000 ) 
        category = 'Big Books';
    else 
        category = "Small Books";
    emit(category, {name: this.name});
};


map说明: 里面会调用emit(key, value),集合会按照指定的key进行映射分组, 类似group by
        上面map后的结果为: (按照category分组, 分组结果是{name: this.name}的list)

{"big books",[{name: "apue"}, {name : "linux book"}, {name : "clrs"}]]);
{"small books",[{name: "python book"}, {name : "golang book"}]);


写reduce函数

var reduce = function(key, values) {
    var sum = 0;
    values.forEach(function(doc) {
    sum += 1;
    });
    return {books: sum};
};

reduce说明:简化函数,会对map分组后的数据进行分组简化,

注意:在reduce(key,value)中的key就是emit中的key,value为emit分组后的emit(value)的集合, 这里是{name: this.name}的list


写mapReduce函数

> var count  = db.books.mapReduce(map, reduce, {out: "book_results"});
> db[count.result].find()
{ "_id" : "big books", "value" : { "books" : 3 } }
{ "_id" : "small books", "value" : { "books" : 2 } }

 如下运行可以看详细信息

> db.books.mapReduce(map, reduce, {out: "book_results"});
{
        "result" : "book_results",
        "timeMillis" : 107,
        "counts" : {
                "input" : 5,
                "emit" : 5,
                "reduce" : 2,
                "output" : 2
        },
        "ok" : 1,
}
> db.book_results.find()
{ "_id" : "big books", "value" : { "books" : 3 } }
{ "_id" : "small books", "value" : { "books" : 2 } }




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值