如何在Mongooese数组中填充字段?

问题描述

我有一个这样的收集模式:

const PrescriptionSchema = new Schema({
    patientId:{type: mongoose.Schema.ObjectId,ref: 'User'},prescriptionName: String,prescriptionNote: String,prescriptionDate: Date,diseaseId: {type: mongoose.Schema.ObjectId,ref: 'Diseases'},drugs: [{
        drugcateId:{type: mongoose.Schema.ObjectId,ref: 'DrugCategory'},quantity: Number,howtouse: String
    }],is_deleted: {type: Boolean,default: false},createdBy: {type: mongoose.Schema.ObjectId,ref: 'User'}
},{
        timestamps: true,collection: 'prescriptions'
    }
);
export default mongoose.model('prescriptions',PrescriptionSchema );

请注意,在此集合中,我有一系列“药物”,这是我从“ DrugCategory”中获得的一系列药物,在每种药物中我都有数量和使用方法..以便将其分组(drugCateId,数量(如何使用)在药品数组中。 找到处方时,我想在Drugs数组中填充drugCateId。我该怎么做? 下面是我当前的代码:

async findOne(req,res){
        const { id } = req.params;
       await Prescriptions.
            findOne({ is_deleted: false,_id: id}).
             .populate(
               {
                 path: 'drugs',populate: {
                     path: 'drugCateId',model: 'DrugCategory'
                   }
               }).
            exec(function (err,data) {
              if (err) return handleError(err);
              res.send(data)
            });
      }

但这是行不通的。 以下是我的结果:

{
    "is_deleted": false,"_id": "5f32768a06693a520806717d","patientId": "5c80930d447df7735138693e","prescriptionName": "Prescription for you","prescriptionNote": "Please drink follow doctor","prescriptionDate": "2020-07-08T00:00:00.000Z","diseaseId": "5f22a6d600980c081ca9e14f","drugs": [
        {
            "_id": "5f32768a06693a5208067181","drugcateId": "5f23deca04e3150a48632229",// How can I populate this one?
            "quantity": 10,"howtouse": "drink 2 times after meal everyday"
        },{
            "_id": "5f32768a06693a5208067180","drugcateId": "5f23deca04e3150a48632233",{
            "_id": "5f32768a06693a520806717f","drugcateId": "5f23deca04e3150a48632234",{
            "_id": "5f32768a06693a520806717e","drugcateId": "5f23deca04e3150a4863224a","howtouse": "drink 2 times after meal everyday"
        }
    ],"createdBy": "5d1cd947231ceb95b8838c1b","createdAt": "2020-08-11T10:44:26.842Z","updatedAt": "2020-08-11T10:44:26.842Z","__v": 0
}

希望您能理解我的问题,请看一看。谢谢你

解决方法

您似乎很亲近,但请确保提供正确的路径:

.populate({
   path: 'drugcateId',model: 'DrugCategory'
})

drugCateId更改为drugcateId

此外,您应该可以直接填充DrugCategory

,

您当前的版本与您的架构不匹配。建议您同时填充drugsdrugs.drugcateId。但是这里drugs是直接嵌入的,不能引用另一个集合中的文档,因此不可能populate()

 .populate({
     path: 'drugs',// this assumes drugs are objectIds: [{ type: ObjectId,ref: 'Drug' }]
     populate: {
       path: 'drugCateId',model: 'DrugCategory'
     }
   })

相反,您应该能够对数组使用点符号:

.populate({ path: "drugs.drugcateId" })

或简称

.populate("drugs.drugcateId")
,

感谢您的回复。我有一个错误,我的模型是“ drugcate”,但在我的填充命令中是“ drugCate”。我已经将模型更改为“ drugCate”,并且可以进行以下查询。

populate({ path: 'drugs.drugCateId',populate: {path: 'drugs.drugCateId'}
}) 

谢谢

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...