我如何填充猫鼬模型内的数组

问题描述

文档:

//在类别模式内

"category": {
        "_id": "5f50be253fe4b520fee4114e","name": "health Care","subCategories": [
            {
                "_id": "5f50c2a4b90c0724832a1d7e","name": "Shop"
            },{
                "_id": "5f50c2a4b90c0724832a1d7f","name": "health tonic"
            },{
                "_id": "5f50c2a4b90c0724832a1d80","name": "protin"
            },],},

产品架构

const productSchema = new mongoose.Schema({
subCategory : {
type: mongoose.Schema.Types.ObjectId,}
})

存储在数据库中 子类别:5f50c2a4b90c0724832a1d7f,

如何填充子类别

解决方法

const categories = await Category.find({}).populate({ path: subCategories,model: subCategory })

详细了解Populate

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...