I have designed a mongodb schema that A plan has multiple milestones, each milestones has multiple tasks, each task is associated with a external Task document. Here is my schema design of mongodb implemented in mongoose
var PlanSchema = new mongoose.Schema({
plan:[ Milestone ]
});
var Milestone = mongoose.Schema({ goal:String,tasks: [ { type: ObjectId, ref: 'Task' } ]});
Now the goal is to find all the tasks of certain milestone in a plan,
PlanModel.findOne({'_id':pid },{ 'plan':{ $slice: [ milestone_number, 1 ] }, 'plan.tasks':1,'plan.goal':1 },function(err, doc){
if(err){callback(err, null);}
else{
callback(null, doc);
}
})
It works, but obviously the tasks retrieved are just a array of Task ObjectID, what I want is the task documents, so I tried the populate methods
PlanModel.findOne({'_id':pid },{ 'plan':{ $slice: [ number, 1 ] }, 'plan.tasks':1,'plan.goal':1 }).populate('Task', 'task_description ').exec( function(err, doc){
if(err){callback(err, null);}
else{
callback(null, doc);
}
})
But all I get is still the array of Task ObjectID, rather than the array of Task documents. Is there anything I do wrong with the populate methods in mongoose?