I am running a map reduce from within the mongoDB shell, and getting outputs that vary in different environments.
The mongoose model is as follows:
preferenceModel = new mongoose.Schema({
user: {type: String, required: true},
activity: {
lastRead: {_id: false, type: Date, required: false},
lastUpdated: {_id: false, type: Date, required: false}
},
layout: [
{
_id: false,
directive: {type: String, required: true},
size: {type: String, required: true, default: 'medium'},
config: {type: Object, default: {}},
userData: {type: Object, default: {}}
}
]
});
the map reduce being run is as follows:
//get positions of cards
db.preferences.mapReduce(
//map
function() {
for(var i = 0; i < this.layout.length; i++) {
var key = this.layout[i].directive;
var value = {
position: i,
user: this.user
};
emit(key, value);
}
},
//reduce
function(key, values) {
positions = [], users = [];
for(i = 0; i < values.length; i+=1) {
var position = positions.push(JSON.stringify(values[i].position));
var user = users.push(JSON.stringify(values[i].user));
}
//
// return newValues;
return {users: [positions, users]};
},
{ out: "cardPositions"}
);
On smaller datasets (hundreds) the output is as defined in the reduce function, but on larger datasets (300,00+), the output is nested what seems like randomly, and furthermore seems to do it every hundred or so documents.
By nested output I mean: Expected: { users: [[0, 5, 4], 50] } Something similar to actual output: { users: users: [[0, 5, 4], 50] }
Sometimes it will nest even more, and on the large data set it seems to be more something like:
{ users: users: [[0, 5, 4], 50] ..., users: [[2, 5, 0], 70] }
with users repeating itself a lot
I have spent a lot of time attempting to debug this issue and any help on advising on this would be great.