【问题标题】:How to populate field of an embedded document?如何填充嵌入文档的字段?
【发布时间】:2022-01-23 10:29:48
【问题描述】:

我有一个 OrderSchema,其中包含“Invoice”作为嵌入式架构。我想从嵌套模式中填充一个字段(“系列”)。

架构如下所示:

    const OrderSchema = new Schema({
        success: {
            type: Boolean,
        },
        invoice: {
            type: new Schema({
                series: {
                    // NEEDS TO POPULATE
                    type: Schema.Types.ObjectId,
                    ref: "Series",
                    required: true,
                },
                number: {
                    type: Number,
                    required: true,
                },
            }, { 
                _id: false,
                timestamps: false 
            }),
            required: true,
        },
    });

在这里,我需要填充路径“invoice.series”。我怎样才能做到这一点?

【问题讨论】:

    标签: mongodb mongoose mongodb-query mongoose-populate


    【解决方案1】:

    你可以像这样填充它

      OrderModel.find(query)
      .populate({ 
         path: 'invoice',
         populate: {
           path: 'series',
         } 
      })
      .exec(function(err, docs) {});
    

    或者您可以选择这样做...

    OrderModel.find(query)
          .populate("invoice.series")
          .exec(function(err, docs) {});
    

    【讨论】:

      猜你喜欢
      • 2021-07-26
      • 2014-08-23
      • 2012-04-16
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 2021-05-19
      • 1970-01-01
      • 2013-06-18
      相关资源
      最近更新 更多