无法在“嵌套”地图实体中填充ObjectId

时间:2019-09-11 14:16:45

标签: dictionary mongoose nested populate

在MongooseMap类型的字段中填充子文档时遇到一些问题。 我使用的是mongoose 5.6.11,而“ new” mongooseMap类型对我很有用...我想;)

这是我的模式

const eproStudyInfoSchema = new Schema({
    gStudyId: {type: String, required: false},
    sStudyId: {type: String, required: false},
    studyId: {type: String, required: true, unique:true},
    title:{type: String, required: true}
}, {_id: true});


const studyEntity4UserSchema = new Schema({
    studyInfo:{ type: Schema.Types.ObjectId, ref: 'eproStudyInfoSchema' },
    role: {type: Number, required: true, default: EEProStudyRoles.user},
    userIdInStudy: {type: String, required: false},
    permission: {type: Number, required: false},
        ...
});

const eproSchema= new Schema({
    studies: {
        type: Map,
        of: studyEntity4UserSchema 
    }
});

const userSchema = new Schema({
      userName: {type: String, required: true},
      userId: {type: String, unique: true, required: true},
      epro:{type: eproSchema, required: false},
...
});

在填充我的数据库之后,“用户”集合部分看起来像这样


{"_id":"5d5eb45795aaf555a80da51b",
"userId":"demo_pat2","__v":0,
"userName":"Demo1-Patient 2",
   "epro":{
       "_id":"5d5eb44942a1e42098132ef0",
      "studies":{
         "epro_demo__v1"{
               "role":10, 
               "_id":"5d78dc75da130529ccf20f9f", 
               "studyInfo":"5d6e97c395aaf555a80e96da", 
               "userIdInStudy":"demo_pat2"}}}
}

和相应的'studyInfo'-集合部分看起来像这样

{"_id":"5d6e97c395aaf555a80e96da",
"studyId":"epro_demo__v1",
"__v":0,
"gStudyId":"epro_demo",
"sStudyId":"v1",
"title":"ePRO - Demonstration - v1"
}

...我现在如何在地图实体中填充 studyInfo

我已经尝试过 deep-populate -概念,但是在我看来,这仅适用于嵌套数组而不适用于map ...或者我在这里出错了吗? >

let user = await this.eproUserModel.findOne({userId: userId})
.populate({
         path:  'epro.studies',
         populate:{
             path: 'studyInfo',
         model: this.eproStudyInfoModel
         }
    })

...对我有用的是,当我声明ObjectId-Field的完整路径时。

    .populate({
        path: 'epro.studies.epro_demo__v1.studyInfo',
        model: this.eproStudyInfoModel
    })

但这对我没有用,因为我不知道每个map-entitiy-key,而且我想找到一个更通用的解决方案,例如对array进行深度填充。

目前在猫鼬中这可能吗?

预先感谢

0 个答案:

没有答案