5

I have a Message Collection which contains messages, models look like this.

var MessageSchema = new mongoose.Schema({
    groupId: { type: Schema.ObjectId, ref: 'Group' },
});

var GroupSchema = new mongoose.Schema({
   type: String,
   groupMembers: [{ "user": { type: Schema.ObjectId, ref: 'User' } }],
});

Here is my code:

 Message.find({ 'groupId': { $in: groupIds } })
.populate(
{ path: 'groupId', select: 'groupMembers type name level', 
populate: { path: 'groupMembers.user', select: 'name _id photo', model: 'User' } })

How can I populate groupMembers.user only if groupId.type matches with a condition?

I've tried this but :-(

{match:"groupId.type":'individual'}
Talha Awan
  • 4,573
  • 4
  • 25
  • 40
Rijad Rahim
  • 409
  • 4
  • 12
  • Try `Message.find({ 'groupId': { $in: groupIds } }).populate({ path: 'groupId', match: { type: 'indiviual' }, select: 'groupMembers type name level', populate: { path: 'groupMembers.user', select: 'name _id photo', model: 'User' } })` – Mikey Jun 21 '17 at 08:31
  • @Mikey: thanks for the comment, I tried that, but **groupId** returns null for other types. – Rijad Rahim Jun 21 '17 at 08:43

0 Answers0