I am using mongoose framework to communicate with the mongodb. Now i am in the situation to join the two collections using mapReduce. I have followed this tutoria "http://blog.knoldus.com/2014/03/12/easiest-way-to-implement-joins-in-mongodb-2-4/" to get it done. I have successfully done with join using mapReduce in mongoDB Shell using robomongo. Same i am trying with mongoose frame work but its giving me error that Out parameter must be defied.
The code sample what i have done.
This is the collection schema for User profile:
var User = new mongoose.Schema({
name : {
first : {type : String},
last : {type : String}
},
title : {type : String},
userName : {type : String, unique : true},
profileImgUrl : {type : String},
mojo : Number
});
this is the collection schema for the testimonials:
var Testimonials = new mongoose.Schema({
from : String,
to : String,
text : String
});
This is the code using mongoose, nodejs:-
var mapTalent = function () {
var output= {userName : this.userName,firstname:this.name.first, lastname:this.name.last , profileImgUrl : this.profileImgUrl, mojo : this.mojo, text : null}
emit(this.userName, output);
};
var mapTestimonial = function () {
var output = {fromTalentName : this.fromTalentName, firstname:null, lastname:null, profileImgUrl : null, mojo : null, text : this.text}
emit(this.text, output);
};
var reduceF = function(key, values) {
var outs = {firstname:null, lastname:null , profileImgUrl:null, text : null, mojo:null};
values.forEach(function(v){
if(outs.firstname ==null){
outs.firstname = v.firstname
}
if(outs.lastname ==null){
outs.lastname = v.lastname
}
if(outs.profileImgUrl ==null){
outs.profileImgUrl = v.profileImgUrl
}
if(outs.mojo ==null){
outs.mojo = v.mojo
}
if(outs.text == null){
outs.text = v.text
}
});
return outs;
};
result = Testimonials.mapReduce(mapTestimonial, reduceF, {out : {reduce : "Talent_Testimonials"}});
result = Talent.mapReduce(mapTalent, reduceF, {out : {reduce : "Talent_Testimonials"}});
Here the error is thrown as " the out option parameter must be defined".
What i am doing wrong here i am not getting. This same works in mongoDB shell.
mapReduce can't be called the same way in Mongoose as it is in the shell.
For Mongoose, the call would need to look like:
Testimonials.mapReduce({
map: mapTestimonial,
reduce: reduceF,
out : {reduce : "Talent_Testimonials"}
}, function (err, results) { ... });
See the docs here.