0
  {
    orders: {
      userID: { type: Schema.Types.ObjectId, ref: 'User' },
      order: [
        {
          productID: { type: Schema.Types.ObjectId, ref: 'Inventory' },
          quantity: { type: Number },
          vendorCoupon: { type: String },
        },
      ],
     }
  }

I have an orderSchema something like this. I can populate userID like that :

Order.find({ userID: req.body.userID }).populate('userID') 
 ... some other code snippet ...

But how should I populate the productID inside this order array ? I have to map all the productID's in the array. How can I implement it ?

Chirag Jain
  • 201
  • 3
  • 8

1 Answers1

0

Try this,

Order.find({ userID: req.body.userID }).populate('userID').populate({
  path: 'order.productID'
});

See also: https://mongoosejs.com/docs/populate.html#deep-populate & Populate nested array in mongoose

v1shva
  • 1,261
  • 3
  • 13
  • 28