2014-10-19 12 views
3

我有三種模式,程序(程序),層(層),報價(報價)。一個程序可以有多個層次,一個層次可以有多個要約。所以我的程序有一系列的層次,相應的一層可以有一系列的優惠。現在我試圖填充程序如下:Mongoose中的多級人員不起作用

var Program = mongoose.model('Program'); 
var Offer = mongoose.model('Offer'); 
var Tier = mongoose.model('Tier'); 

Program.findOne({ 
     _id: p_id 
    }).populate('tiers').exec(function(err, docs){ 
     var opts = { 
      path: 'tiers.offers' 
     } 
     Program.populate(docs, opts, function(err, docs){ 
      console.log('populated'); 
//   var s = require('util').inspect(docs, {depth : null}) 
      console.log(docs); 
      console.log(docs.tiers[0]); //Printing complete tier information 
      console.log(docs.tiers[0].offers[0]) //Just printing Object ID, not taking data from offers table 
     }) 
    }) 

問題是它只是填充層而不是提供。我如何更深入?相應的例子是在這裏:

https://github.com/paulcsmith/mongoose-currency/blob/master/node_modules/mongoose/examples/population-across-three-collections.js

回答

1

我有一個類似的問題,我有一個富人混合(下一頁歌曲),還藝術家的歌曲。在這裏,首先我找到名字的歌曲,然後填充下一首歌曲以及他們的藝術家,然後填入歌曲,然後填充歌曲以及歌手信息。

希望它有幫助。

Song.find({songName: req.params.id}) 
    .lean() 
    .populate({ path: 'songMixs songArtist' }) 
    .exec(function(err, docs) { 

    var options = { 
     path: 'songMixs.nextSong', 
     model: 'Song' 
    }; 

    if (err) return res.json(500); 
    Song.populate(docs, options, function (err, projects) { 

     var options2 = { 
      path: 'songMixs.nextSong.songArtist', 
      model: 'Artist' 
     }; 

     Artist.populate(docs, options2, function (err, projects) { 
      res.json(projects); 
     }); 
     //res.json(projects); 
    }); 
    }); 
+0

此行'.populate({path:'songMixs songArtist'})'確實幫助我理解了如何一次填充2條路徑......非常感謝! – natureminded 2017-11-14 02:35:17