简体   繁体   English

将新项目添加到MongoDB数组时出错

[英]Error while adding new item to MongoDB array

I am trying to add a new item to Treatment array.It's basically to store the list of procedures offered by a particular hospital.Hospital id is a unique id for a hospital and its created by a post api along with the list of treatments offered by that hospital.Later when hospital introduce a new surgical procedure,i need to add that into the treatment array,for that i have created a put API 我正在尝试向``治疗阵列''中添加一个新项目,它基本上是存储特定医院提供的程序列表。医院ID是医院的唯一ID,由post api和医院提供的治疗列表一起创建那家医院。以后医院采用新的手术程序时,我需要将其添加到治疗阵列中,为此我创建了一个put API

I am getting the following error whenever I call the below snippet in API 每当我在API中调用以下代码段时,都会收到以下错误

The positional operator did not find the match needed from the query. Unexpanded update: Treatment.$.procedureid

Please help me to resolve it. 请帮我解决。

You can find the model and controller code below: 您可以在下面找到型号和控制器代码:

Schema definition 模式定义

var hospitalDoctorSchema = new Schema({
    hospitalID: { type: Number, required: true, unique: true, dropDups: true, default: 10000 },   
    Treatment: [{        
        procedureid: { type: Number},
        departmentId: { type: Number},
                name: { type: String, required: true, trim: true },
                costUpperBound: { type: Number, required: true },
                costLowerBound: { type: Number, required: true },                   
                departmentName: { type: String, required: true, trim: true},


    }],
    updated_at: { type: Date, required: true, default: Date.now }
});

Controller code to add new item to treatment array: 将新项目添加到处理数组的控制器代码:

 hospitalModel.findOneAndUpdate({ "hospitalID": 10000}, { "$set": {"Treatment.$.procedureid"=1100}, { returnOriginal: false, upsert: true }, function (err, doc) {
    if (err) {
        logger.error("Error while updating record : - " + err.message);
        return res.status(500).json({ "Message": err.message });
    } else if (doc === null) {
        logger.error("Error while updating record : - Hospital not found in database");
        return res.status(408).json({ "Message": "Hospital not found in database" });
    }   

    res.status(200).json({ "Message": "Hopsital details for " + hospitalName + " have been updated successfully" });
});

If you would like to add a new element to the array then you can use the $push operator like the following: 如果要向数组添加新元素,则可以使用$ push运算符,如下所示:

hospitalModel.findOneAndUpdate({ "hospitalID": 10000}, 
{ "$push": {"Treatment" : { "procedureid" : 1100 } } }, 
{ returnOriginal: false, upsert: true }, function (err, doc) { ... });

If you want to add a new element in array. 如果要在数组中添加新元素。 You have to use $push instead of $set . 您必须使用$ push而不是$ set Because $push which is used to insert element and $set which used only for updation. 因为$ push用于插入元素,而$ set仅用于更新。 So 所以

hospitalModel.findOneAndUpdate({ "hospitalID": 10000},{"$push": 
{"Treatment" : { "procedureid" : 1100 } } }, { returnOriginal: false, 
upsert: true }, function (err, doc) { ... });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM