So it may be that mapreduce is not the best way to handle this but here's what I am trying to achieve.
I have a schema of events and a schema of venues. Each event has multiple dates and 0/1 venue. I want, on event save, to generate entries for each possible date and store that alongside the venue info in another collection - say occurrences. But I have 2 questions/issues.
I seem to not be able to call external things from the map function even when using scope so my call to find the venue info fails.
Where is the best place to deal with generating an entry for each date? Loop through dates in map and emit for each date? or in the reduce or finalize function?
Rather than just tell me that I cannot call a dbref from a mapreduce function I would prefer to find the most efficient solution to create, on event save, a collection of occurrences that contain the needed event/venue data. I am open to other methods but the code below should outline my needs.
Event.pre('save', function (next) {
var o = {};
o.scope = {Venue: mongoose.model.Venue};
o.map = function () {
var data = {dates: this.dates};
var self = this;
if(this.venue) {
Venue.view(this.venue, {}, function(err, doc) {
if(doc) {
data.venue_name = doc.name;
}
emit(self._id, data);
});
} else {
emit(this._id, data);
}
};
o.reduce = function (k, vals) {
return vals;
};
o.out = { replace: 'occurrences' };
o.verbose = true;
mongoose.models.Event.mapReduce(o, function (err, model, stats) {
next();
});
});
map/reduce operates on one collection at a time. My suggestion would be: