Morgante Orion Pell
Morgante Orion Pell

Reputation: 75

MongoDB merge one field into existing collection with Map/Reduce

I have a MongoDB database with 2 collections:

All changes to groups are done by changing the members array of the group to include the users ids.

I want to sync these changes across to the users collection by using map/reduce. How can I output the results of map/reduce into an existing collection (but not merging or reducing).

My existing code is here: https://gist.github.com/morgante/5430907

Upvotes: 4

Views: 518

Answers (1)

Gates VP
Gates VP

Reputation: 45287

How can I output the results of map/reduce into an existing collection

You really can't do it this way. Nor is this really suggested behaviour. There are other solutions:

Solution #1:

  • Output the map / reduce into a temporary collection
  • Run a follow-up task that updates the primary data store from the temporary collection
  • Clean-up the temporary collection

Honestly, this is a safe way to do this. You can implement some basic retry logic in the whole loop.

Solution #2:

  • Put the change on a Queue. (i.e. "user subscribes to group")
  • Update both tables from separates workers that are listening for such events on the queue.

This solution may require a separate piece (the queue), but any large system is going to have such denormalization problems. So this will not be the only place you see this.

Upvotes: 2

Related Questions