javascript - MongoDB 减少单值键

标签 javascript mongodb mapreduce

我想统计客户的订单数量,因此,在去年、上个月和上周。 我写了一个 MapReduce 程序:

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" }
    }
)

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

{
    "_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
            }
            ...
        }
}

只有 1 个订单的客户不通过 reduce 函数。 我在解释该行为的 MongoDB 文档中发现了这一点:

MongoDB will not call the reduce function for a key that has only a single value.

我怎样才能在我的输出集合中只有一种类型的记录,看起来像这样?强制所有记录通过 reduce 函数?

{
    "_id" : 80306,
    "value" : {
        "orders_count" : {
            "total" : {
                "count_1year" : 18,
                "count_1month" : 6,
                "count_7day" : 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();

关于javascript - MongoDB 减少单值键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35290213/

相关文章:

file - 我可以在 hdfs (hadoop) 中复制数据吗

javascript - 按钮点击根据一些参数改变div内容

hadoop - Hadoop-MapReduce集群中此数据存储库的大小不断增加

eclipse - Hadoop eclipse插件错误

Mongodb 查找并插入

c# - 如何通过一次调用使用 mongodb C# 驱动程序添加嵌套元素或更新属性

mongodb - xDB 不存储任何交互

javascript - 异步 Javascript 程序如何交互

javascript - 将 bool 表达式简化为一个表达式

javascript - C3图表在 Angular Directive(指令)中调整大小