Alex
Alex

Reputation: 2382

MongoDB map-reduce

How do I query another collection in the reduce step based on the results of the map?

I have a name/type combination in one collection, and I need to add some fields to this collection as a look-up from another collection on a this combination. I was under the impression that it's possible, but the documentation says that reduce or map cannot access the database for any reason.

I can do this in code, but was wondering if i can use the map-reduce interface directly in mongo.

Basically, the steps are:

  1. map over collection1 and return two fields
  2. look up values from collection2 and insert back into collection1.

Upvotes: 0

Views: 95

Answers (1)

Asya Kamsky
Asya Kamsky

Reputation: 42352

You cannot query another collection during reduce phase.

Basically, reduce exists to combine all collective emitted output values for each unique key you are aggregating by.

There are no joins or pseudo-joins in MongoDB so this all has to be done on the application side.

Upvotes: 1

Related Questions