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

时间:2020-09-06 07:50:20

标签: node.js mongoose populate

文档:

//在类别模式内

"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,

如何填充子类别

1 个答案:

答案 0 :(得分:1)

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

详细了解Populate