我尝试在mongodb中运行一个map/reduce函数,其中我按集合中对象中包含的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时,我不能按列分隔day、uid或destination,因为映射将这些字段组合在一起。
我想要的是这样的:
{ { "day" : "2012-4-9" }, { "uid" : "1234456" }, { "destination" : "Home"}, { "count" : 1 } }这有可能吗?
顺便说一句--我能够通过将sed应用于文件并清理CSV来使输出工作。更多的工作,但它起作用了。如果我能以正确的格式从mongodb中获得它,那将是最理想的。
发布于 2012-07-05 20:02:51
MapReduce仅返回{_id:some_id, value:some_value}格式的文档
请参阅:How to change the structure of MongoDB's map-reduce results?
https://stackoverflow.com/questions/10403204
复制相似问题