Tiered population in Mongoose does not work
I have three models, programs (Program), levels (Tier), offers (Offer). A program can have multiple levels, and levels can have multiple sentences. Thus, my program has an array of tiers, and accordingly, a layer can have an array of sentences. Now I am trying to populate programs like this:
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
})
})
The problem is that this is just populating levels, not proposals. How do I go deeper? A relevant example is given below:
+3
source to share
1 answer
I had a similar problem, I have a song that has mixes (Next songs) as well as artists. Here, first I find the song by name, then I fill in the songs that go on, as well as their performers, in the song that goes on, I fill in the song as well as information about the artist.
Hope it helps.
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);
});
});
+1
source to share