8

My schema:-

var playlistSchema = new Schema({
name        : {type:String,require:true},
videos      : {type:[mongoose.Schema.Types.ObjectId],ref: 'Video'},
},{collection:'playlist'})

var Playlist = mongoose.model('Playlist',playlistSchema);

I have some data in the database as exapmple:-

{
"_id" : ObjectId("58d373ce66fe2d0898e724fc"),
"name" : "playlist1",
"videos" : [ 
    ObjectId("58d2189762a1b8117401e3e2"), 
    ObjectId("58d217e491089a1164a2441f"), 
    ObjectId("58d2191062a1b8117401e3e4"), 
    ObjectId("58d217e491089a1164a24421")
],
"__v" : 0

}

And the schema of Video is :-

var videoSchema = new Schema({
name        : {type:String,required:true},
createdAt   : {type:String,default:new Date()},
isDisabled  : {type:Boolean,default:false},
album       : {type: mongoose.Schema.Types.ObjectId, ref: 'Album'}
},{collection:'video'})

var Video = mongoose.model('Video',videoSchema);

Now in order to get the name of the all the videos in playlist i am trying the code:-

 var playlistModel = mongoose.model('Playlist');
let searchParam = {};
searchParam._id = req.params.pid;
playlistModel.findOne(searchParam)
.populate('[videos]')
.exec(function(err,found){
    if(err)
        throw err;
    else{
        console.log(found.videos[0].name);
    }
})

But here i am getting the undefined result.I am not getting where i am wrong plzz anyone help me to short out this problem.

1
  • 1
    Why do you use [ ] around videos. I don't think that's required. Commented Mar 23, 2017 at 12:52

2 Answers 2

17

got the answer:- just change the schema

var playlistSchema = new Schema({
name        : {type:String,require:true},
videos      : [{type:mongoose.Schema.Types.ObjectId,ref: 'Video'}],
},{collection:'playlist'})

var Playlist = mongoose.model('Playlist',playlistSchema);

and just use

.populate('videos')

instead of

.populate('[videos]')
Sign up to request clarification or add additional context in comments.

Comments

0
const app = await this.appModel.findOne({ slug })
    .populate({
        path: 'category', 
        populate: {
           path: 'subCategories',
           model: 'Category',
           select: { name: 1, slug: 1, _id: 0 }
        },
     });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.