samedi 18 avril 2015

MongoDB MapReduce weird bug

I'm trying this simple MapReduce operation:



function map() {

var gameDay = Math.floor((this.matchCreation - 1427846400000) / 86400000) + 1; // day of april 2015 when the game was played


this.teams.forEach (function (team){

**team.bans.forEach(function (ban){** // says bans is undefined

var value ={
banned : 1,
firstBanned: ( ((ban.pickTurn == 1) || (ban.pickTurn == 2))? 1 : 0 )

}

emit({championId: ban.championId,
day: Number(gameDay)}, value);
emit({championId: ban.championId,
day: "all"}, value);

});

});

}

function reduce(key, values) {

var a = values[0];

for(var i = 1 ; i<values.length ; i++){

var b = values[i]; // will merge 'b' into 'a'

a.banned += (b.banned? b.banned : 0);
a.firstBanned += (b.firstBanned? b.firstBanned : 0);

for (var attrname in b){

if(attrname != "banned" && attrname != "firstBanned")
a[attrname] = b[attrname];

}

}

return a;

}

matchesCollection.mapReduce(map, reduce, {
out: { reduce: "mapReduceResults" }

}, function (err, data){

if(err)
return callback (err);


callback (null, "OK");


});


It used to work before, but just when I tried to deploy the app after testing for a while, it seems to fail in this line: team.bans.forEach(function (ban){, says team.bans is undefined, although every one of the documents has a "teams" array and a "bans" array inside of it, I even double checked it by querying the database and there is no document in which those fields dont exist.


So weird. The reduce function is a bit more complex but it seems to work alright, yet the map one (unlike Reduce, its supposed to be called just once per original document, right?) throws this unexplainable error. Could anyone give me some insight?


Aucun commentaire:

Enregistrer un commentaire