PUKHRAJ SAINI
PUKHRAJ SAINI

Reputation: 11

how can i populate an array inside mongoose model

document:

// inside Category Schema

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

Product Schema

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

stored in db subCategory: 5f50c2a4b90c0724832a1d7f,

how can populate Subcategory

Upvotes: 1

Views: 35

Answers (1)

Tahero
Tahero

Reputation: 328

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

Read more about Populate

Upvotes: 1

Related Questions