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

问题描述

文档:

//在类别模式内

"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