2

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 !!

  • Possible duplicate of [Mongoose populate with array of objects containing ref](https://stackoverflow.com/questions/16641210/mongoose-populate-with-array-of-objects-containing-ref) – ambianBeing Oct 28 '19 at 13:36

2 Answers2

3

You should be able populate like this:

Sub_cat.find({}).populate("selection_data.selection_id")
Shihab
  • 2,641
  • 3
  • 21
  • 29
1

try this:

Sub_cat.find({}).select('selection_data').populate({path:'selection_id', model:"Selection")
Babak Abadkheir
  • 2,222
  • 1
  • 19
  • 46