I have the following schemas:
var userSchema = new Schema({
username: String,
rooms: [{type: Schema.Types.ObjectId, ref: 'Room'}]
});
var roomSchema = new Schema({
members: [
{
user:{type:Schema.Types.ObjectId, ref 'User'},
role: String
}
],
messages: [
{
text: String,
meta: {
send: {type: Schema.Types.ObjectId, ref 'User'},
sent_time: Date
}
}
]
});
I wish to obtain all rooms of a particular user and deep populate each room to get populated messages and members arrays so that each sender in a message is populated with username and each member is populated with his/her username, something like this:
User.findById(id).
populate({
path: 'rooms',
populate: [
{
path: 'members.user',
select: 'username'
},
{
path: 'messages.meta.sender',
select: 'username'
}
]
}).exec(function(err, self) { // self becomes deep populated});
Obviously, the above didn't work for me.