简体   繁体   中英

Mongoose populate subdocument in array

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?

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

The alternative solution you can embed sections into the order schema

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM