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

cauchuyennhocuatoi

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

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", // How can I populate this one?
            "quantity": 10,
            "howtouse": "drink 2 times after meal everyday"
        },
        {
            "_id": "5f32768a06693a520806717f",
            "drugcateId": "5f23deca04e3150a48632234", // How can I populate this one?
            "quantity": 10,
            "howtouse": "drink 2 times after meal everyday"
        },
        {
            "_id": "5f32768a06693a520806717e",
            "drugcateId": "5f23deca04e3150a4863224a", // How can I populate this one?
            "quantity": 10,
            "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
}

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

伊皮

您当前的版本与您的架构不匹配。建议您同时填充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")

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在C中填充char数组

来自分类Dev

如何在JNA中填充结构数组?

来自分类Dev

如何在c中填充指针数组

来自分类Dev

如何在javascript中填充数组?

来自分类Dev

如何在中填充数组列表

来自分类Dev

如何在结构数组中添加字段?

来自分类Dev

如何在mongodb中查询数组字段?

来自分类Dev

如何在空数组中添加字段?

来自分类Dev

如何在结构数组中添加字段?

来自分类Dev

如何在laravel中插入数组字段

来自分类Dev

如何在Twig中填充数组的数组?

来自分类Dev

如何在Swift中创建自动填充文本字段

来自分类Dev

如何在订阅费用中填充Stripe的描述字段?

来自分类Dev

Rails:如何在Rails中预填充文本字段?

来自分类Dev

如何在填充的输入字段/框中对齐文本?

来自分类Dev

如何在字典理解中填充可选字段?

来自分类Dev

如何在SwiftUI中自动填充文本字段?

来自分类Dev

如何在SQL中填充标题字段

来自分类Dev

如何在 Swift 中从 UIPicker 填充文本字段

来自分类Dev

如何在多个输入字段中填充以下对象?

来自分类Dev

如何在java中动态填充jsonArray中的数组

来自分类Dev

流星:如何自动使用存储在集合中其他字段中的数组长度填充字段?

来自分类Dev

流星:如何自动使用存储在集合中其他字段中的数组长度填充字段?

来自分类Dev

如何在记录数组的数组中的字段上分组?

来自分类Dev

如何在Julia中按行填充数组

来自分类Dev

如何在关联数组JavaScript中填充值

来自分类Dev

如何在Python中声明和填充数组?

来自分类Dev

如何在C中填充2D结构数组?

来自分类Dev

如何在JavaScript中动态填充多维数组?

Related 相关文章

热门标签

归档