猫鼬填充数组中的子文档

Mongoose populate subdocument in array

我有一个 Mongoose 报价模型,解释如下:

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

和参考下面解释的第一个模式报价的订单模式:

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

这里无法填充部分的问题{section: { type: Schema.Types.ObjectId, ref: 'Offer.sections' }}

它给了我 MissingSchemaError: Schema hasn't been registered for model "Offer.sections".

那么有什么方法可以填充部分吗?

很遗憾,Mongoose 不支持此功能。 检查 Github 问题 here

您可以将 部分 嵌入到订单架构中的替代解决方案