本文介绍了如何在猫鼬中填充嵌套实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下猫鼬模式结构
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 模型进行查找的同时填充所有这些嵌套文档?
How can i populate all these nested documents while doing a find on the USER model?
我尝试使用如下填充
User.find(ctx.request.query).populate(
{path: 'roles.role'
,populate: { path: 'roleEntities.entity'}
}).
exec()
但它没有解析 roleEntities.entity
but it's not resolving roleEntities.entity
推荐答案
你可以尝试链式填充操作
You can try chaining populate operations
User.find()
.populate("roles.role")
.populate("roles.role.roleEntities.entity")
这篇关于如何在猫鼬中填充嵌套实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!