I have this setup in my MongoDB
Items:
title: String
comments: [] // of objectId's
Comments:
user: ObjectId()
item: ObjectId()
comment: String
Here's my Mongoose schema:
itemSchema = mongoose.Schema({
title: String,
comments: [{ type: Schema.Types.ObjectId, ref: 'comments' }],
});
Item = mongoose.model('items', itemSchema);
commentSchema = mongoose.Schema({
comment: String,
user: { type: Schema.Types.ObjectId, ref: 'users' },
});
Comment = mongoose.model('comments', commentSchema);
This is where I get my items along with the comments:
Item.find({}).populate('comments').exec(function(err, data){
if (err) return handleError(err);
res.json(data);
});
How do I populate the comments array with it's respective user? Since each comment has a user ObjectId()?