db.runCommand({
mapreduce: "somePreAggregatedCollection",
query: {
"start" : {
$gt: previousRun,
}
},
map : function Map() {
delete this.start;
delete this.end;
delete this._id;
var key = this.user,
value = this;
delete value.user;
emit(key, value);
},
reduce : function Reduce(user, aggregationData) {
var result = {};
aggregationData.forEach(function(timespan){
Object.keys(timespan).forEach(function(field){
if (!result[field] || (field.indexOf('Last', field.length - 5) != -1)) {
result[field] = timespan[field];
} else if ((field.indexOf('Count', field.length - 5) != -1) || (field.indexOf('Sum', field.length - 5) != -1)) {
result[field] += timespan[field];
} else if (field.indexOf('Min', field.length - 5) != -1) {
result[field] = (result[field] > timespan[field]) ? timespan[field] : result[field];
} else if (field.indexOf('Max', field.length - 5) != -1) {
result[field] = (result[field] < timespan[field]) ? timespan[field] : result[field];
}
});
});
return result;
},
sort : { "user" : 1, "start" : 1 },
out : { reduce: "lifetime_agg" },
jsMode: true
counts: {
input: 0,
emit: 0,
reduce: 0,
output: number of records already in lifetime_agg
}
I would have expected some sort of an error message, and the whole thing still works if I force it to run with a smaller query result.
P.S. jsMode is set to true because I was tweaking with it and the number of records is nowhere near 500,000. It behaves the same way with jsMode set to false.