Please, I need to undertand ASAP Mongoose way to map reduce.
I already had some succes with the map part but not being able to generate
the map_reduce_collection in the right way. It seems to just doing a
passthru from the map to the final result without reducing.
Any clue or link will be really appreciated.
Regards.
Leo
_____________________________________________________________________________________________________________________
var Answer = require('../models/answer');
mongoose.connect('mongodb://localhost/MyDB');
var mapFunction = function() {
emit(this.questionId, {response: this.response, questionId:
this.questionId}); //sends the url 'key' and a 'value' to the reduce
function
}
var reduceFunction = function(key, value) { //reduce function
var result = {};
result.summary= [0,0, 0, 1000] ;
console.dir(key);
console.dir(value);
console.dir(result);
for (index in value) {
var doc = value[index];
console.log('response:' + doc.response);
result.summary[0] += (doc.response=='Y'? 1 : 0);
result.summary[1] += (doc.response=='N'? 1 : 0);
result.summary[2] += (doc.response=='N/A'? 1 : 0);
result.summary[3] -= (doc.response!=null ? 1 : 0);
}
result.questionId = key;
console.log('leaving reduce');
return result;
};
var command = {
mapreduce: "answers", //the name of the collection we are
map-reducing *note,
//this is the model Ping we defined
above...mongoose automatically
//appends an 's' to the model name within
mongoDB
map: mapFunction.toString(),
reduce: reduceFunction.toString(),
query: { surveyId:'1', responseType: 'su', userType:'l,s' },
//sort: {$natural: -1 }, Seems not to be working at all any way we
have chance to sort in the last find
out: 'map_red' //the collection that will contain the map-reduce
results
};
mongoose.connection.db.executeDbCommand(command, function(err, dbres) {
if(err){
console.error(err);
}else {
console.log('No erros in first executeDbCommand');
console.dir(dbres.documents);
console.dir(dbres.documents[0]);
mongoose.connection.db.collection('map_red', function(err,
collection) { //query the new map-reduced table
if(err) {
console.error(err);
} else {
console.log('We are closer!');
collection
.find({})
.sort({'_id': -1})
//.limit(10)
.toArray(function(err, map_reduce_result) { //only pull in the
top 10 results and sort descending by number of pings
if(err) {
console.error(err);
} else{
console.log('But the map_reduce_result is not what
expected, it\'s just the result of map without the reduce part. WTF!!!');
console.dir(map_reduce_result);
}
});
}
});
}
//If you need to alert users, etc. that the mapreduce has been run,
enter code here
});
_____________________________________________________________________________________________________________________
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en