我想计算客户的订单数量,因此,在去年,上个月和上周。我写了一个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" }
}
)
在我的输出集合中,我有 2 种类型的记录
{
"_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不会为只有 单个值。
如何使输出集合中只有 1 种类型的记录如下所示?强制所有记录通过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();
使用 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
}
)