1

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

1 Answer 1

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

Read more about Populate

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.