mongoDB mapreduce 花了很长时间 运行 3m 文档

mongoDB mapreduce taking so long time for running 3m document

我有一个包含 300 万个文档的集合。每个文档有 40 个字段。字段如下所示。

{
    "b_date" : "2016-04-05",
    "d_date" : "2016-06-25",
    "pos" : "MISC",
    "origin" : "DXB",
    "destination" : "HGA",
    "pax" : 1,
    "pax_1" : 2
 },   
{
    "b_date" : "2016-04-05",
    "d_date" : "2016-06-25",
    "pos" : "MISC",
    "origin" : "DXB",
    "destination" : "HGA",
    "pax" : 4,
    "pax_1" : 5
 },   
{
    "b_date" : "2016-04-05",
    "d_date" : "2016-06-26",
    "pos" : "MISC",
    "origin" : "DXB",
    "destination" : "HGA",
    "pax" : 3,
    "pax_1" : 3
 }

现在我想通过分组 b_dated_dateposorigin 来得到 paxpax_1 的总和, destination 个字段。 cumulative pax 是 posorigindestination 字段的分组,但 cumulative pax 和 pax_1 应根据 b_dated_date 的升序增加.

Expected result is.

{
    "_id.dep_date" : "2016-04-05",
    "_id.sale_date" : "2016-06-25",
    "_id.pos" : "MISC",
    "_id.origin" : "DXB",
    "_id.destination" : "HGA",
    "value.pax" : 5,
    "value.cumulative_pax":5,
    "value.pax_1" : 7,
    "value.cumulative_pax_1":7,

 },   
{
    "_id.dep_date" : "2016-04-05",
    "_id.sale_date" : "2016-06-26",
    "_id.pos" : "MISC",
    "_id.origin" : "DXB",
    "_id.destination" : "HGA",
    "value.pax" : 3,
    "value.cumulative_pax":8,
    "value.pax_1" : 3,
    "value.cumulative_pax_1":10,
 }

我的 mapReduce 代码

db.collection.mapReduce(
function() {
    emit(
    {
    "pos" : this.pos,
    "origin" : this.origin,
    "destination" : this.destination,
    'dep_date': this.d_date,
    'sale_date': this.b_date,

},
    {
        'pax':this.pax,
        'pax_1':this.pax_1,
    }
    );
}
,
function(key,values) {
    paxt = 0;
    paxt_1 = 0;
    for (var i in values){
    paxt += values[i].pax;
    paxt_1 += values[i].pax_1;
    }
    return {'pax':paxt,
    'pax_1':paxt_1,
    };
}
,
{
    'scope':{
        'pos':'',
        'origin':'',
        'destination':'',
        'dep_date': '',
        'sale_date': '',
        'result':{}
    }
    ,
    'finalize':function(key,value) {
        if (pos != key.pos || 
            origin != key.origin || 
            destination != key.destination || 
            ){  
            result['pax'] = 0;
            result['pax_1'] = 0;
            result['cumulative_pax'] = 0;
            result['cumulative_pax_1'] = 0;
        }
            result['pax'] += value.pax;
            result['cumulative_pax'] = value.pax;
            result['pax_1'] += value.pax_1;
            result['cumulative_pax_1'] = value.pax_1;
            pos = key.pos;
            origin = key.origin;
            destination = key.destination;
            dep_date = key.dep_date;
            sale_date = key.sale_date;

        return result;
        }
    ,
    'out':'some_collection'
    }
)

这张地图减少了返回的预期值,但它花了很多时间,比如 3 个小时。是因为 'b_date' 和 'd_date' 是字符串格式的日期吗?或者如何优化。
聚合在 3 分钟内返回结果,但我无法通过使用聚合获得累积 pax。

Map Reduce代码,

db.collection.mapReduce(
function() {
    emit(
    {
    "pos" : this.pos,
    "origin" : this.origin,
    "destination" : this.destination,
    'dep_date': this.d_date,
    'sale_date': this.b_date,

},
    {
        'pax':this.pax,
        'pax_1':this.pax_1,
    }
    );
}
,
function(key,values) {
    paxt = 0;
    paxt_1 = 0;
    for (var i in values){
    paxt += values[i].pax;
    paxt_1 += values[i].pax_1;
    }
    return {'pax':paxt,
    'pax_1':paxt_1,
    };
}
,
{
    'scope':{
        'pos':'',
        'origin':'',
        'destination':'',
        'dep_date': '',
        'sale_date': '',
        'result':{}
    }
    ,
    'finalize':function(key,value) {
        if (pos != key.pos || 
            origin != key.origin || 
            destination != key.destination || 
            ){  
            result['pax'] = 0;
            result['pax_1'] = 0;
            result['cumulative_pax'] = 0;
            result['cumulative_pax_1'] = 0;
        }
            result['pax'] += value.pax;
            result['cumulative_pax'] = value.pax;
            result['pax_1'] += value.pax_1;
            result['cumulative_pax_1'] = value.pax_1;
            pos = key.pos;
            origin = key.origin;
            destination = key.destination;
            dep_date = key.dep_date;
            sale_date = key.sale_date;

        return result;
        }
    ,
    'out':'some_collection'
    }
)