Mongoose populate subdocument in array

Tags: , , , ,



I have a Mongoose offer model explained below:

const OfferSchema = new Schema({
  sections: [
    {
      title: String,
    },
  ],
});

and order schema which has reference to to the first schema offer explained below:

const OrderSchema = new Schema({
  offers: [
    {
      offer: { type: Schema.Types.ObjectId, ref: 'Offer' },
      sections: [
        {
          section: { type: Schema.Types.ObjectId, ref: 'Offer.sections' }, // issue here
        },
      ],
    },
  ],
});

the problem that I can not populate sections here {section: { type: Schema.Types.ObjectId, ref: 'Offer.sections' }}

it gives me MissingSchemaError: Schema hasn't been registered for model "Offer.sections".

so is there any way to populate sections?

Answer

Unfortunately, Mongoose doesn’t support this feature. check the Github issue here

The alternative solution you can embed sections into the order schema



Source: stackoverflow