Mongo 数据库:更新文档的某些字段以及在一个 FindbyIdandUpdate 中推送引用

Mongo DB : update some field of document as well push reference in one FindbyIdandUpdate

感谢您的帮助,我是 Mongo 数据库的新手,我正在通过引用实现多对多关系。

注意:我知道如何在不同的通话中做到这一点我想通过最佳实践在一次通话中进行更新

用例:医生和专业(多对多)。

当医生更新时,比如在他的个人资料中添加专业,也会更新一些个人信息,比如 Phone、State City。

必填 它的引用数组需要与其字段一起更新。 此外,专业数组需要由医生更新为专业参考数组。

博士架构

const doctorSchema = new Schema({
_id : mongoose.Schema.Types.ObjectId,
email : {
    type : String,
    required : true,
    unique : true,
    lowercase: true,
    validate: value => {
        if (!validator.isEmail(value)) {
            throw new Error({error: 'Invalid Email address'})
        }
    }
},
Name : {
    type : String
},
password : {
    type : String,
    required : true,
    minLength : 7
},
gender : {
    type : String
},
address : {
    type : String
},
state : {
    type : String
},
city : {
    type : String
},
zip : {
    type : Number
},
phone : {
    type : Number,
    required : true,
    unique : true
}doctorSpecialities : [{
    type : Schema.Types.ObjectId,
    ref  : "Specialty"
}]};

专业架构

const specialtySchema = new Schema({
_id : mongoose.Schema.Types.ObjectId,
name :{
    type : String
},
specialtiesDoctors : [{
    type :  Schema.Types.ObjectId,
    ref  :  'Doctor'
}]
});

module.exports = mongoose.model('Specialty', specialtySchema);

医生更新资料通话

updateDoctorInfo : async (req, res) => {
    /** mongoose _id validation */
    if(!mongoose.isValidObjectId(req.params.id)){
        return res.status(400).json({
            "Success" : false,
            "Message" : 'Doctor Id wrong format'
        })
    }
    /** mongoose _id Speciality Validation */
    if(!mongoose.isValidObjectId(req.body.speciality)){
        return res.status(400).json({
            "Success" : false,
            "Message" : 'Speciality Id wrong format'
        })
    }
    /** updating Doctor's Profile */
    let doctor = {};
    doctor.username        = req.body.username,
    doctor.phone            = Date.parse(req.body.DOB),
    doctor.gender          = req.body.gender,
    doctor.address         = req.body.address,
    doctor.state           = req.body.state,
    doctor.city            = req.body.city,
    doctor.zip             = Number(req.body.zip),
    doctor.speciality      = req.body.speciality,
    

    Doctor.findByIdAndUpdate(req.params.id, doctor, {new: true}, function (err, doctor) {
        if (err){
            return res.status(500).send("There was a problem updating the user.");  
        }
        return res.status(200).send(doctor);
    });
},

我只想更新专业中的参考数组和本次调用中的医生。

请帮助我卡住了将近 2 天。

Doctor.findByIdAndUpdate(req.params.id,{$set: req.body,$push: { doctorSpecialities:  req.body.speciality} }, {new: true}, function (err, doctor) {
        if(err){
            return res.status(400).send("Error" + err);  
        }else {
            Specialty.findByIdAndUpdate(req.body.speciality,{$push: { specialtiesDoctors:  doctor._id}}, {new: true}, function (err, speciality) {
                if(!err){
                    return res.status(200).send({'Success' : true , 'Data' : doctor});
                }
            });
        }
    });