Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

populating a key in an array of objects

I am a new user to mongodb and have a doubt regarding how to populate particular key of every object in an array . here is the schema of the collection i want to populate

var Sub_cat = new mongoose.Schema({
    cat_id:{
        type: mongoose.Schema.Types.ObjectId,
        ref: "Cat"
    },
    name: String,
    quantity_type: String,
    selection_data:[{
        price:Number,
        selection_id:{
            type: mongoose.Schema.Types.ObjectId,
            ref: "Selection"
        }
    }]
});

here i wish to populate selection_id in selection_data array which i wish to further populate. can someone please help me out . Thank you !!

like image 497
Sambhav jain Avatar asked Mar 03 '26 23:03

Sambhav jain


1 Answers

You should be able populate like this:

Sub_cat.find({}).populate("selection_data.selection_id")
like image 184
Shihab Avatar answered Mar 06 '26 21:03

Shihab