MongoDB使用单值减少密钥

时间:2016-02-09 10:55:56

标签: javascript mongodb mapreduce

我想计算客户的订单数量,从而计算去年,上个月和上周的订单数量。 我写了一个MapReduce程序:

bindings

在我的输出集合中,我有两种类型的记录

var mapOrders = function() {
    var v_order = {
        order_date : this.dt_order
        ...
    }; 

     emit(this.clientid, v_order);
};

var reduceOrders = function(p_clientid, p_orders) {
    // Initialization of the output format of the couters
    var r_result = { orders_count : {
        total: {
            1year: 0,
            1month: 0,
            7day: 0
        }
        ...
    }}

    for (var c_order = 0; c_order < p_orders.length; c_order++) {
        // Increment counters
    }

    return (r_result);
};

db.orders.mapReduce(
    mapOrders,
    reduceOrders,
    { 
        out: { merge:  "tmp_orders_indicators" }
    }
)

只有1个订单的客户端不会通过reduce功能。 我在MongoDB doucmentation中找到了解释这种行为的内容:

  

MongoDB不会为只有一个密钥的密钥调用reduce函数   单一价值。

如何才能在输出集合中只有一种类型的记录?强制所有记录都通过reduce函数?

{
    "_id" : 80320,
    "value" : {
        "order_date" : ISODate("2015-10-30T11:09:51.000Z")
        ...
    }
}

{
    "_id" : 80306,
    "value" : {
        "orders_count" : {
            "total" : {
                "count_1year" : 18,
                "count_1month" : 6,
                "count_7day" : 1
            }
            ...
        }
}

2 个答案:

答案 0 :(得分:1)

您可以通过聚合无缝地实现这一目标。考虑以下管道:

var dateSevenDaysAgo = new Date();
dateSevenDaysAgo.setDate(dateSevenDaysAgo.getDate()-7);

var dateMonthAgo = new Date();
dateMonthAgo.setMonth(dateMonthAgo.getMonth()-1);

var dateYearAgo = new Date();
dateYearAgo.setFullYear(dateYearAgo.getFullYear()-1);

var pipeline = [
    { "$match": { "$dt_order": { "$gte": dateYearAgo } } },
    {
        "$group": {
            "_id": "$id_client",
            "count_1year": {
                "$sum": {
                    "$cond": [ 
                        { "$gte": [ "$dt_order", dateYearAgo ] }, 
                        1, 0 
                    ]
                }
            },
            "count_1month": {
                "$sum": {
                    "$cond": [ 
                        { "$gte": [ "$dt_order", dateMonthAgo ] }, 
                        1, 0 
                    ]
                }
            },
            "count_7day": {
                "$sum": {
                    "$cond": [ 
                        { "$gte": [ "$dt_order", dateSevenDaysAgo ] }, 
                        1, 0 
                    ]
                }
            }
        }
    },
    { "$out": "tmp_indicators" }
];

db.orders.aggregate(pipeline);
db.tmp_indicators.find();

答案 1 :(得分:1)

使用finalize实用程序找到解决方案。

var mapOrders = function() {
    var v_order = {
        order_date : this.dt_order
        ...
    }; 

     emit(this.clientid, v_order);
};

var reduceOrders = function(p_clientid, p_orders) {
    // Initialization of the output format of the couters
    var r_result = { orders_count : {
        total: {
            1year: 0,
            1month: 0,
            7day: 0
        }
        ...
    }}

    for (var c_order = 0; c_order < p_orders.length; c_order++) {
        // Increment counters
    }

    return (r_result);
};


var finalizeOrders = function(p_clientid, p_ReducedDrders) {

    if (typeof p_ReducedDrders.orders_count === 'undefined' )
        // Initialization of the output format of the couters
        var r_result = { orders_count : {
            total: {
                1year: 0,
                1month: 0,
                7day: 0
            }
        ...
        }}

        // do the same stuff as the for loop in the reducer
    }
    else {
        r_result = p_ReducedDrders
    }

    return (r_result);
};

db.orders.mapReduce(
    mapOrders,
    reduceOrders,
    { 
        out: { merge:  "tmp_orders_indicators" },
        finalize : finalizeOrders
    }
)