2

I have a collection of events that look like

{
  _id: BSONID
  name: "event_name",
  values: {a: 10, b: 1000, c: 50}
}

I'm trying to use mapReduce them using

map = function() {
  return emit([this.name, this.values['a']], this.values['b']);
}

reduce = function(key, values) {
  // stuff
}

collection.mapReduce(map, reduce, { out: { inline: 1 } }, callback);

However, I would like to be able to dynamically change which values I map against. In essence, I'd like to have

var key = 'a';
var value = 'b';

map = function ()
{
  return emit([this.name, this.values[key]], this.values[value]);
}

The problem is that the execution context isn't passed to mongodb. Any solution that doesn't rely on using string for functions?

1 Answer 1

2

Yes, you can pass a "scope" variable to MapReduce:

scope = {key : "a", value : "b"};
collection.mapReduce(map, reduce, {scope : scope, out: { inline: 1 } }, callback);
Sign up to request clarification or add additional context in comments.

1 Comment

how can you reach the context inside ::map ? -> this.context ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.