我有以下mongoose架构结构
userSchema = new Schema({
roles: [
role: {type: Schema.Types.ObjectId, ref: 'Role' }
]
})
rolesSchema = new Schema({
name: String,
roleEntities: [
{
entity : {type: Schema.Types.ObjectId, ref: 'RoleEntity' },
abilities : [{type: Schema.Types.ObjectId, ref: 'Ability' }]
}
]
}
roleEntitiesSchema = new Schema({
name: String
})
abilitiesSchema = new Schema({
name: String
})
如何在USER模型上执行查找时填充所有这些嵌套文档?
我尝试使用populate,如下所示
User.find(ctx.request.query).populate(
{path: 'roles.role'
,populate: { path: 'roleEntities.entity'}
}).
exec()
但它没有解决roleEntities.entity
答案 0 :(得分:7)
您可以尝试链接填充操作
User.find()
.populate("roles.role")
.populate("roles.role.roleEntities.entity")
答案 1 :(得分:4)
猫鼬4:
User
.find()
.populate({
path: 'roleIds',
model: 'roles',
populate: {
path: 'otherIds',
model: 'other'
}
})
答案 2 :(得分:2)
对我来说,以下工作
.populate({
path: 'favorites.favorite',
model: 'Joke',
populate: {
path: 'user',
model: 'User',
},
答案 3 :(得分:1)
这是嵌套在多个对象/数组内部的深层人口的一个极端示例:
Deal.find()
.populate({
path: 'fund',
populate: [{
path: 'organizer',
populate: {
path: 'banking.accounts.transactions.associatedInvestment',
model: 'Investment'
}
}, {
path: 'documents'
}]
})