1

我正在尝试运行一个map/reduce函数,在该函数中mongodb,我按集合中对象中包含的 3 个不同字段进行分组。我可以运行 map/reduce 函数,但是所有发出的字段在输出集合中一起运行。我不确定这是否正常,但输出数据进行分析需要更多的工作来清理。有没有办法将它们分开,然后使用mongoexport

让我告诉你我的意思:

我尝试分组的字段是日期、用户 ID(或 uid)和目的地。

我运行这些功能:

map = function() {
    day = (this.created_at.getFullYear() + "-" + (this.created_at.getMonth()+1) + "-" + this.created_at.getDate());
    emit({day: day, uid: this.uid, destination: this.destination}, {count:1});
}

/* Reduce Function */
reduce = function(key, values) { 
    var count = 0;
    values.forEach(function(v) {
        count += v['count'];
}
);
  return {count: count};
}

/* Output Function */
db.events.mapReduce(map, reduce, {query: {destination: {$ne:null}}, out: "TMP"});

输出如下所示:

{ "_id" : { "day" : "2012-4-9", "uid" : "1234456", "destination" : "Home" }, "value" : { "count" : 1 } }
{ "_id" : { "day" : "2012-4-9", "uid" : "2345678", "destination" : "Home" }, "value" : { "count" : 1 } }
{ "_id" : { "day" : "2012-4-9", "uid" : "3456789", "destination" : "Login" }, "value" : { "count" : 1 } }
{ "_id" : { "day" : "2012-4-9", "uid" : "4567890", "destination" : "Contact" }, "value" : { "count" : 1 } }
{ "_id" : { "day" : "2012-4-9", "uid" : "5678901", "destination" : "Help" }, "value" : { "count" : 1 } }

当我尝试使用mongoexport时,我无法按列分隔日期、uid 或目的地,因为地图将这些字段组合在一起。

我想要的看起来像这样:

{ { "day" : "2012-4-9" }, { "uid" : "1234456" }, { "destination" : "Home"}, { "count" : 1 } }

这甚至可能吗?

顺便说一句 - 我能够通过应用sed到文件并清理 CSV 来使输出工作。更多的工作,但它奏效了。mongodb如果我能以正确的格式 把它弄出来,那将是理想的。

4

1 回答 1

1

MapReduce 只返回表单的文档{_id:some_id, value:some_value}

请参阅:如何更改 MongoDB 的 map-reduce 结果的结构?

于 2012-07-05T12:02:51.277 回答