【问题标题】:Create ref to sub document's array for each property of subdocument为子文档的每个属性创建对子文档数组的引用
【发布时间】:2021-12-02 11:22:59
【问题描述】:

模型A看起来像

    { a : {
     step1: [{
        type: { type: String },
        category: { type: String },
       }],
     step2: [{
        type: { type: String },
        category: { type: String },
       }]
    } }

我想创建的模型 B 应该包含一个引用模型 A.step1 或 A.step2 的道具,试图通过以下方式实现这一目标

    { progress : [
         {
           step: {
              type: Schema.Types.ObjectId,
              ref: "A.a.{What should be here}",
              required: true,
           },
           details: { type: Schema.Types.Mixed },
         }
      ]
    }

【问题讨论】:

    标签: mongodb mongoose mongodb-query mongoose-schema mongoose-populate


    【解决方案1】:

    我认为您需要为所有这些创建模式 :)

    我只是将这三者完全分开 - 不确定这对你是否可行,因为这背后的整个想法有点神秘,但这样的东西对你有用吗?

    const stepSchema = new Schema({
        type: String,
        category: String,
    });
    const Step = mongoose.model("steps", stepSchema);
    
    
    const aSchema = new Schema({
        step1: [stepSchema],
        step2: [stepSchema],
    });
    const A = mongoose.model("as", aSchema);
    
    
    const progressSchema = new Schema({
        a: { type: aSchema, required: true, ref: "as"},
        progress: [{ type: stepSchema, required: true, ref: "steps" }],
        details: Schema.Types.Mixed,
    });
    const Progress = mongoose.model("progresses", aSchema);
    

    【讨论】:

      猜你喜欢
      • 2019-09-13
      相关资源
      最近更新 更多