使用Mongoose填充嵌套数组
我有一个用户架构,如下所示,我正在尝试填充实时项目数组,但我无法弄清楚如何访问它。
const userSchema = new Schema({
local: {
email: String,
username: String,
password: String,
liveProjects: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'liveProject'
}]
},
google: {
googleId: String,
email: String,
username: String,
liveProjects: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'liveProject'
}]
}
});
const User = mongoose.model('user', userSchema);
module.exports = User;
如果它没有嵌入我可以使用
User.findById(id).populate('liveProjects').exec((err,projects)=>{});
但是如何才能访问'local.liveProjects'
或'google.liveProjects'
以便我可以填充它们?
事实证明它就是这么简单
User.findById(id).populate('local.liveProjects').exec((err,projects)=>{});