Is it possible to update the mango collection from the map finalization method to reduce the engine?

I tried to pass the collection to update as a scope variable - no dice. I tried to call db.getCollection

from the body finalization - without dice, I get this:

db assertion failure, assertion: 'invoke failed: JS Error: TypeError: db has no properties nofile_b:18', assertionCode: 9004

      

I assume this means db

undefined within the finalize method. So, is this possible?

EDIT

Here is my finalize method:

function(key, value) {
  function flatten(value, collector) {
    var items = value;
    if (!(value instanceof Array)) {
      if (!value.items) {
        collector.push(value);
        return;
      }

      items = value.items;
    }
    for (var i = 0; i < items.length && collector.length < max_group_size; ++i) {
      flatten(items[i], collector);
    }
  }

  var collector = [];
  flatten(value, collector);
  return collector;
}

      

I would like to replace collector.push(value)

with an insert into some collection.

+3


source to share


1 answer


There is no way to modify another collection from within the Map / Reduce / Finalize function.

Here is a link to a question from a user with a similar question. The answer, unfortunately, is no.
How can I change the structure of MongoDB map display results?

The reason for this is that MapReduce is designed to run in a closed environment. The computations are distributed among the different shards and the results are then aggregated. If each function running on each shard was allowed to modify the collections, then each shard could receive different data.



If you want an individual collection to be modified as a result of a Zoom Out Map operation, the best strategy is to run a Zoom Out Map operation, get the results, and then update the individual collection.

If you want the results of several map reduction operations to be combined, you can do this by fading out the map. Documentation on this can be found here: http://www.mongodb.org/display/DOCS/MapReduce#MapReduce-IncrementalMapreduce

+2


source







All Articles