MongoDB Map / Reduce仅在内联模式下工作

时间:2012-03-13 16:30:38

标签: mongodb mapreduce

我编写了map / reduce / finalize函数,在我的测试中使用out:{inline:true}选项正常工作。 但是如果我尝试使用out:{replace:'test'}(或merge,..)将结果保存在集合中,它将不会向我显示相同的结果。

有没有人有线索,我做错了什么?

THX

内联:

db.runCommand({mapreduce: 'source', map: map_deliverstat, reduce: reduce_deliverstat, finalize: finalize_deliverstat, out: {inline:1}})

{
"_id": {
    "date": ISODate("2012-03-13T00:00:00Z"),
    "customerid": 469
},
"value": {
    "date": ISODate("2012-03-13T00:00:00Z"),
    "customerid": NumberLong(469),
    "sum": 294,
    "nomarker": 42,
    "marker": 252,
    "product1": 34,
    "product2": 22,
    "product3": 20,
    "product4": 19,
    "product5": 16
}
}

替换:

db.runCommand({mapreduce: 'source', map: map_deliverstat, reduce: reduce_deliverstat, out: {replace: 'test'}, finalize: finalize_deliverstat})

{
"_id": {
    "date": ISODate("2012-03-13T00:00:00Z"),
    "customerid": 469
},
"value": {
    "date": ISODate("2012-03-13T00:00:00Z"),
    "customerid": NumberLong(469),
    "sum": 2,
    "nomarker": 0,
    "marker": 2,
    "product1": 0,
    "product2": 0,
    "product3": 0,
    "product4": 0,
    "product5": 0
}
}

1 个答案:

答案 0 :(得分:1)

另一种运行地图缩小的方法,根据您的需要进行修改,看看这是否适合您

以下查询按州分列客户数

map = function() {
 emit({state: this.CustomerState}, {count:1})
}

reduce = function(key, values) {
  var count = 0;

  values.forEach(function(v) {
    count += v['count'];
  });

  return {state: key,count: count};
}
db.createCollection('map_temp')
db.customer_info.mapReduce(map, reduce, {out:'map_temp'})

您将结果保存在map_temp集合