Deep Fill with Condition

I have a collection of messages that contains messages, the 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 the condition?

I tried this, but: - (

{match:"groupId.type":'individual'}
+4
source share

Source: https://habr.com/ru/post/1679781/


All Articles