Mongodb map reduce across 2 collection

旧街凉风 提交于 2019-12-06 07:24:31

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.

You have a few options:

Option #1: Embed the age with the vote.

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

Option #2: Keep a counter of the ages

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

Option #3: Do a "manual" join

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

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.

My suggestion

Go with #1 or #2.

Kuroro

Instead of

{name:'a', age:12}

It is easier to add a new field to user document and maintain it in each vote update.Of course, you can enjoy to use map reduce to analysis your data.

{name:'a', age:12, voteTitle:["Title1","Title2","Title3","Title4"]}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!