如何分组mongodb - mapReduce输出? [英] How to Group mongodb - mapReduce output?

查看:110
本文介绍了如何分组mongodb - mapReduce输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于mongodb中的mapReduce框架的查询,所以我有一个mapReduce函数的键值对的结果,现在我想在mapReduce的这个输出上运行查询。



所以我使用mapReduce来查找像这样的用户的统计信息。
$ b $ $ $ $ $ $ $ $ $ $ db.order.mapReduce(function (){emit(this.customer,{count:1,orderDate:this.orderDate.interval_start})},
function(key,values){
var sum = 0; var lastOrderDate;
values.forEach(function(value){
if(value ['orderDate']){
lastOrderDate = value ['orderDate'];
}
sum + = value ['count'];
});
return {count:sum,lastOrderDate:lastOrderDate};
},
{query:{status:DELIVERED},out :order_total})。find()

给我这样的输出

  {_id:ObjectId(5443765ae4b05294c8944d5b),value:{count:1,orderDate:ISODate(2014 -10-18T18:30:00Z)}} 
{_id:ObjectId(54561911e4b07a0a501276af),value:{count:2,lastOrderDate:ISODate(2015-03-14T18:30:00Z)}}
{_id:ObjectId(54561b9ce4b07a0a501276b1),value:{count:1,orderDate:ISODate(2014-11-01T18:30:00Z)}}
{ _id:ObjectId(5458712ee4b07a0a501276c2),value:{count:2,lastOrderDate:ISODate(2014-11-03T18:30:00Z)}}
{_id: ObjectId(545f64e7e4b07a0a501276db),value:{count:15,lastOrderDate:ISODate(2015-06-04T18:30:00Z)}}
{_id:ObjectId( 54690771e4b0070527c657ed),value:{count:6,lastOrderDate:ISODate(2015-06-03T18:30:00Z)}}
{_id:ObjectId(54696c64e4b07f3c07010b4a) ,value:{count:1,orderDate:ISODate(2014-11-18T18:30:00Z)}}
{_id:ObjectId(546980d1e4b07f3c07010b4d),value :{count:4,lastOrderDate:ISODate(2015-03-24T18:30:00Z)}}
{_id:ObjectId(54699ac4e4b07f3c07010b51),value count:30,la stOrderDate:ISODate(2015-05-23T18:30:00Z)}}
{_id:ObjectId(54699d0be4b07f3c07010b55),value:{count:1,orderDate: ISODate(2014-11-16T18:30:00Z)}}
{_id:ObjectId(5469a1dce4b07f3c07010b59),value:{count:2,lastOrderDate 2015-04-29T18:30:00Z)}}
{_id:ObjectId(5469a96ce4b07f3c07010b5e),value:{count:1,orderDate:ISODate(2014-11 -16T18:30:00Z)}}
{_id:ObjectId(5469c1ece4b07f3c07010b64),value:{count:9,lastOrderDate:ISODate(2015-04-15T18: 30:00Z)}}
{_id:ObjectId(5469f422e4b0ce7d5ee021ad),value:{count:5,lastOrderDate:ISODate(2015-06-01T18:30:00Z )}}
......

现在我想运行查询和根据不同类别的计数对用户进行分组,例如对于一个组中的计数小于5,另一个中的计数为5-10的用户,并且希望输出类似于这

  {userL essThan5:9} 
{user5to10:2}
{user10to15:1}
{user15to20:0}
....

 

code> db.order.mapReduce(function(){emit(this.customer,{count:1,orderDate:this.orderDate.interval_start})},
function(key,values){
var category; //添加这个新字段
var sum = 0; var lastOrderDate;
values.forEach(function(value){
if(value ['orderDate']){
lastOrderDate = value ['orderDate'];
}
sum + = value ['count'];
});
//此时你已经知道你的记录在哪个类别中,只需添加一个新字段来标记它
if(sum< 5){category:userLessThan5};
if(sum> = 5&& sum< = 10){category:user5to10};
if(总和< = 10&& sum> = 15){category:user10to15};
if(总和< = 15&& sum> = 20){category:user15to20};
....
return {count:sum,lastOrderDate:lastOrderDate,category:category};
},
{query:{status:DELIVERED},out:order_total})。find()
db.order_total.aggregate([{$ group:{_id :$ value.category,users:{$ sum:1}}}]);

你会得到你想要的结果

 {userLessThan5:9} 
{user5to10:2}
{user10to15:1}
{user15to20:0}
....


i have a query regarding the mapReduce framework in mongodb, so i have a result of key value pair from mapReduce function , now i want to run the query on this output of mapReduce.

So i am using mapReduce to find out the stats of user like this

db.order.mapReduce(function() { emit (this.customer,{count:1,orderDate:this.orderDate.interval_start}) },
function(key,values){ 
    var sum =0 ; var lastOrderDate;  
    values.forEach(function(value) {
     if(value['orderDate']){ 
        lastOrderDate=value['orderDate'];
    }  
    sum+=value['count'];
}); 
    return {count:sum,lastOrderDate:lastOrderDate}; 
},
{ query:{status:"DELIVERED"},out:"order_total"}).find()

which give me output like this

{ "_id" : ObjectId("5443765ae4b05294c8944d5b"), "value" : { "count" : 1, "orderDate" : ISODate("2014-10-18T18:30:00Z") } }
{ "_id" : ObjectId("54561911e4b07a0a501276af"), "value" : { "count" : 2, "lastOrderDate" : ISODate("2015-03-14T18:30:00Z") } }
{ "_id" : ObjectId("54561b9ce4b07a0a501276b1"), "value" : { "count" : 1, "orderDate" : ISODate("2014-11-01T18:30:00Z") } }
{ "_id" : ObjectId("5458712ee4b07a0a501276c2"), "value" : { "count" : 2, "lastOrderDate" : ISODate("2014-11-03T18:30:00Z") } }
{ "_id" : ObjectId("545f64e7e4b07a0a501276db"), "value" : { "count" : 15, "lastOrderDate" : ISODate("2015-06-04T18:30:00Z") } }
{ "_id" : ObjectId("54690771e4b0070527c657ed"), "value" : { "count" : 6, "lastOrderDate" : ISODate("2015-06-03T18:30:00Z") } }
{ "_id" : ObjectId("54696c64e4b07f3c07010b4a"), "value" : { "count" : 1, "orderDate" : ISODate("2014-11-18T18:30:00Z") } }
{ "_id" : ObjectId("546980d1e4b07f3c07010b4d"), "value" : { "count" : 4, "lastOrderDate" : ISODate("2015-03-24T18:30:00Z") } }
{ "_id" : ObjectId("54699ac4e4b07f3c07010b51"), "value" : { "count" : 30, "lastOrderDate" : ISODate("2015-05-23T18:30:00Z") } }
{ "_id" : ObjectId("54699d0be4b07f3c07010b55"), "value" : { "count" : 1, "orderDate" : ISODate("2014-11-16T18:30:00Z") } }
{ "_id" : ObjectId("5469a1dce4b07f3c07010b59"), "value" : { "count" : 2, "lastOrderDate" : ISODate("2015-04-29T18:30:00Z") } }
{ "_id" : ObjectId("5469a96ce4b07f3c07010b5e"), "value" : { "count" : 1, "orderDate" : ISODate("2014-11-16T18:30:00Z") } }
{ "_id" : ObjectId("5469c1ece4b07f3c07010b64"), "value" : { "count" : 9, "lastOrderDate" : ISODate("2015-04-15T18:30:00Z") } }
{ "_id" : ObjectId("5469f422e4b0ce7d5ee021ad"), "value" : { "count" : 5, "lastOrderDate" : ISODate("2015-06-01T18:30:00Z") } }
......

Now i want to run query and group the users on the basis of count in different categories like for user with count less than 5 in one group , 5-10 in another, etc

and want output something like this

{userLessThan5: 9 }
{user5to10: 2 }
{user10to15: 1 }
{user15to20: 0 }
  ....

解决方案

Try this,

db.order.mapReduce(function() { emit (this.customer,{count:1,orderDate:this.orderDate.interval_start}) },
function(key,values){ 
var category; // add this new field
var sum =0 ; var lastOrderDate;  
values.forEach(function(value) {
 if(value['orderDate']){ 
    lastOrderDate=value['orderDate'];
}  
sum+=value['count'];
}); 
// at this point you are already aware in which category your records lies , just add a new field to mark it
 if(sum < 5){ category: userLessThan5};
 if(sum >= 5 && sum <=10){ category: user5to10};
 if(sum <= 10 && sum >= 15){ category: user10to15};
 if(sum <= 15 && sum >=20){ category: user15to20};
  ....
return {count:sum,lastOrderDate:lastOrderDate,category:category}; 
},
{ query:{status:"DELIVERED"},out:"order_total"}).find()
 db.order_total.aggregate([{ $group: { "_id": "$value.category", "users": { $sum: 1 } } }]);

you will get you desired result

{userLessThan5: 9 }
{user5to10: 2 }
{user10to15: 1 }
{user15to20: 0 }
 ....

这篇关于如何分组mongodb - mapReduce输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆