Mongodb Map减少了2个集合 [英] Mongodb map reduce across 2 collection

查看:158
本文介绍了Mongodb Map减少了2个集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有用户和帖子集.在帖子收集中,将用户名投票存储为密钥.

Let say we have user and post collection. In post collection, vote store the user name as a key.

db.user.insert({name:'a', age:12});
db.user.insert({name:'b', age:12});
db.user.insert({name:'c', age:22});
db.user.insert({name:'d', age:22});

db.post.insert({Title:'Title1', vote:[a]});
db.post.insert({Title:'Title2', vote:[a,b]});
db.post.insert({Title:'Title3', vote:[a,b,c]});
db.post.insert({Title:'Title4', vote:[a,b,c,d]});

我们想按帖子分组.标题并找出不同用户年龄的投票数.

We would like to group by the post.Title and find out the count of vote in different user age.

> {_id:'Title1', value:{ ages:[{age:12, Count:1},{age:22, Count:0}]} }
> {_id:'Title2', value:{ ages:[{age:12, Count:2},{age:22, Count:0}]} }
> {_id:'Title3', value:{ ages:[{age:12, Count:2},{age:22, Count:1}]} }
> {_id:'Title4', value:{ ages:[{age:12, Count:2},{age:22, Count:2}]} }

我已经搜索过,但是找不到在mongodb mapreduce中访问2集合的方法. 是否有可能实现重新还原?

I have searched through and doesn't find a way to access 2 collection in mongodb mapreduce. Could it be possible to achieve in re-reduce?

我知道将用户文档嵌入到帖子中非常简单,但这并不是一个好方法,因为实际的用户文档具有许多属性.如果我们包括用户文档的简化版本,它将限制分析的范围.

I know it is much simple to embedded the user document in post, but it is not a nice way to do as the real user document have many properties. If we include the simplify version of user document, it will limit the dimension of analysis.

{Title:'Title1', vote:[{name:'a', age:12}]}

推荐答案

MongoDB没有多重集合Map/Reduce. MongoDB没有任何JOIN语法,对于临时连接可能不是很好.您将需要以某种方式对这些数据进行非规范化.

MongoDB does not have a multi-collection Map / Reduce. MongoDB does not have any JOIN syntax and may not be very good for ad-hoc joins. You will need to denormalize this data in some way.

您有几种选择:

选项1:将年龄与投票紧密结合.

{Title:'Title1', vote:[{name:'a', age:12}]}

选项2:记录年龄段

{Title:'Title1', vote:[a, b], age: { "12" : 1, "22" : 1 }}

选项3:手动"加入

您的最后一个选择是编写对两个集合执行for循环并正确合并数据的脚本/代码.

Your last option is to write script/code that does a for loop over both collections and merges the data correctly.

因此,您将遍历post并输出带有标题和投票列表的集合.然后,您将遍历新集合并通过查找每个user来更新年龄.

So you would loop over post and output a collection with the title and the list of votes. Then you would loop through the new collection and update the ages by looking up each user.

我的建议

选择#1或#2.

这篇关于Mongodb Map减少了2个集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆