如何使用mongoose更新mongodb中的整个对象

rbl8hiat  于 2023-01-13  发布在  Go
关注(0)|答案(3)|浏览(218)

我正在尝试根据学号更新特定学生的详细信息。我不知道如何使用findById更新整个学生的详细信息。我可以从前端获得更新值并将其发送到服务器,但从服务器我可以将更新值发送到mongoDB,但我不知道如何更新。请帮助我。
这是我的服务器代码:

server.post('/update',urlencodedParser,function(req,res){
var response={
  Id:req.body.Id,
  Fname : req.body.Fname,
  age:req.body.age,
  Lname : req.body.Lname,
  dob : req.body.dob,
  pob : req.body.pob,
  month : req.body.month,
  nation : req.body.nation,
  mothertongue : req.body.mothertongue,
  bloodgroup : req.body.bloodgroup,
  fatherfname : req.body.fatherfname,
  fatherlname : req.body.fatherlname,
  occupation : req.body.occupation,
  placeofwork : req.body.placeofwork,
  officaladd : req.body.officaladd,
  emailid : req.body.emailid,
  phoneno : req.body.phoneno,
  mobileno : req.body.mobileno,
  motherfname : req.body.motherfname,
  motherlname : req.body.motherlname,
  motheroccupation : req.body.motheroccupation,
  motherplaceofwork : req.body.motherplaceofwork,
  motherofficaladd : req.body.motherofficaladd,
  motheremailid : req.body.motheremailid,
  motherphoneno : req.body.motherphoneno,
  mothermobileno : req.body.mothermobileno,
  adress : req.body.adress,
  emergencyadress : req.body.emergencyadress,
  emergencyphone1 : req.body.emergencyphone1,
  emergencyphone2 : req.body.emergencyphone2,
  relationship1 : req.body.relationship1,
  relationship2 : req.body.relationship2

}
databaseInterface.updateStudent(response,  function(err, valss){
  if(err) res.send('ERROR!');
  console.log(valss);
  res.send(valss);
})
})

这是我的 Mongoose 代码:

function updateStudent(response,callback) {
  console.log(response)
    User.findById(response.Id, function(err, studentcollection2) {
        if (err) return callback(err);
            studentcollection2 = response;
        return callback(null, studentcollection2);
         });
    }
qcuzuvrc

qcuzuvrc1#

Mongoose提供了一个特定的查找和更新功能,您缺少的主要瘦是{$set:response}。Mongoose会将存储ID中的每个值替换为response中的相应值。只需确保Mongoose用户架构允许所有这些操作。代码的第二部分应该如下所示:

function updateStudent(response,callback) {
  console.log(response)
    User.findByIdAndUpdate(response.Id, {$set:response},function(err, studentcollection2) {
        if (err) return callback(err);
        return callback(null, 'success');
         });
    }
pzfprimi

pzfprimi2#

这是我想出来的答案。

function updateStudent(response,callback) {
      console.log(response.Id)
      User.update({'Id':response.Id}, {$set:{
      'Firstname':response.Fname,
      'Age' : response.age,
      'Lastname' : response.Lname,
      'DateOfBirth' : response.dob,
      'PlaceOfBirth' : response.pob,
      'Months' : response.month,
      'Nationality' : response.nation,
      'MotherTongue' : response.mothertongue,
      'BloodGroup' : response.bloodgroup,
      'Father.Firstname' : response.fatherfname,
      'Father.Lastname' : response.fatherlname,
      'Father.Occupation' : response.occupation,
      'Father.PlaceOfWork' : response.placeofwork,
      'Father.OfficialAddress' : response.officaladd,
      'Father.EmailId' : response.emailid,
      'Father.PhoneNo' : response.phoneno,
      'Father.MobileNo' : response.mobileno,
      'Mother.Firstname' : response.motherfname,
      'Mother.Lastname' : response.motherlname,
      'Mother.Occupation' : response.motheroccupation,
      'Mother.PlaceOfWork' : response.motherplaceofwork,
      'Mother.OfficialAddress' : response.motherofficaladd,
      'Mother.EmailId' : response.motheremailid,
      'Mother.PhoneNo' : response.motherphoneno,
      'Mother.MobileNo' : response.mothermobileno,
      'ResidentialAddress' :response.adress,
      'EmergencyContactNumber.Address' : response.emergencyadress,
      'EmergencyContactNumber.PhoneNo1' : response.emergencyphone1,
      'EmergencyContactNumber.PhoneNo2' : response.emergencyphone2,
      'EmergencyContactNumber.Relationship1' : response.relationship1,
      'EmergencyContactNumber.Relationship2' : response.relationship2
            }
        }, function(err, studentcollection2) {
          console.log(studentcollection2);
            if (err) return callback(err);
            return callback(null, 'success');
             });
        }
t98cgbkg

t98cgbkg3#

更新整个文档而无需指定每个字段的一种方法如下:

<FUNCTION_NAME>: async (req, res, next) => {
    await <YOUR_MODEL>.findOneAndUpdate(
          {
            _id: <ID_PARAMETER>,
          },
          {
            ...req.body,
          },
          { new: true },
        );
  },

你可能会添加一个尝试捕捉和其他东西,但我想显示明确的东方方式!

相关问题