首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mongodb Map/Reduce - Multiple Group By

Mongodb Map/Reduce - Multiple Group By
EN

Stack Overflow用户
提问于 2012-05-02 03:35:59
回答 1查看 4K关注 0票数 1

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

让我告诉你我的意思:

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

我运行以下函数:

代码语言:javascript
复制
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"});

输出如下所示:

代码语言:javascript
复制
{ "_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,因为映射将这些字段组合在一起。

我想要的是这样的:

代码语言:javascript
复制
{ { "day" : "2012-4-9" }, { "uid" : "1234456" }, { "destination" : "Home"}, { "count" : 1 } }

这有可能吗?

顺便说一句--我能够通过将sed应用于文件并清理CSV来使输出工作。更多的工作,但它起作用了。如果我能以正确的格式从mongodb中获得它,那将是最理想的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-05 20:02:51

MapReduce仅返回{_id:some_id, value:some_value}格式的文档

请参阅:How to change the structure of MongoDB's map-reduce results?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10403204

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档