2014-09-19 60 views
0

我正在使用貓鼬框架與mongodb進行通信。現在我正在使用mapReduce加入兩個集合。我按照這個教程「http://blog.knoldus.com/2014/03/12/easiest-way-to-implement-joins-in-mongodb-2-4/」來完成它。 我已經成功完成了使用robomongo在mongoDB Shell中使用mapReduce的連接。 同樣我試圖與貓鼬框架的工作,但它給我的錯誤,出參數必須無視。Mongoose mapReduce錯誤:必須定義OUT參數

代碼示例我做了什麼。

這對於用戶資料的收集模式:

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 
}); 

這是爲見證收集模式:

var Testimonials = new mongoose.Schema({ 
    from : String, 
    to : String, 
    text : String 
    }); 

這是一個用貓鼬的代碼,的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"}}); 

這裏引發的錯誤是「out option option parameter must be defined」。

我在做什麼錯在這裏我沒有得到。這同樣適用於mongoDB shell。

回答

0

mapReduce不能在Mongoose中調用與在shell中相同的方式。

對於貓鼬,通話將需要看起來像:

Testimonials.mapReduce({ 
    map: mapTestimonial, 
    reduce: reduceF, 
    out : {reduce : "Talent_Testimonials"} 
}, function (err, results) { ... }); 

請參閱該文檔here

相關問題