map-reduce to join when multiple results share same join key

7,587 views
Skip to first unread message

Michael Siegel

unread,
Feb 21, 2012, 10:50:25 AM2/21/12
to mongodb-user
Hi,

Is there a way to use map-reduce to join between collections where the
output of the join involves multiple joined documents with the same
value for the key used? The join examples I’ve found seem to all
involve joining between two collections where there is a 1-1
relationship between documents in the collections. This doesn’t seem
to allow for a more general Cartesian product filtered by the join
condition, since the reduce function can only output one document for
any given key.

For example, say there are two collections:

A, whose documents have fields a and b
B, whose documents have fields b and c

Is there a way to perform an inner join on these two collections, over
field b equality, even if there is a value of b that occurs more than
once in at least one of the input collections? Does this sort of
operation just not fit within the Mongo paradigm?

Thanks,
Michael

Barrie

unread,
Feb 21, 2012, 11:17:50 PM2/21/12
to mongodb-user
Hi Michael,

I believe what you're looking for is done on the client side using
multiple queries, not the server.

Map/reduce isn't a tool that can be used to replace joins, but people
are doing interesting things to bring together multiple collections
which have a common key, which you may find useful here:
http://tebros.com/2011/07/using-mongodb-mapreduce-to-join-2-collections/

Hope this helps.

Barrie

Michael Siegel

unread,
Mar 5, 2012, 4:40:18 PM3/5/12
to mongodb-user
Ah, thank you for the clarification and the link. This makes sense.
I realized recently that I think I had started wondering about this
question after seeing the title of: http://rickosborne.org/download/SQL-to-MongoDB.pdf

...But, I looked at it more closely, and indeed there is only one
table in the FROM clause it's translating from.
Reply all
Reply to author
Forward
0 new messages