2017-02-15 87 views
0

我想根据学生id更新这里的特定学生详细信息。我不知道如何使用findById更新整个学生的详细信息。 我能够从前端获取更新值并将其发送到服务器,但从服务器我能够发送更新值到mongoDB,但我不知道如何更新。 请帮我一把。如何使用猫鼬更新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); 
}) 
}) 

这是我的猫鼬代码:

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); 
     }); 
    } 

回答

0

这是我想出的答案。

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'); 
      }); 
     } 
0

猫鼬一种特定的功能来查找和更新。您缺少的主要功能是{$ set:response}。猫鼬将用响应中的相应值替换存储的ID中的每个值。只要确保您的Mongoose Schema for User将允许所有这些。你的代码的第二部分应该是这样的:

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'); 
     }); 
    } 
+0

它返回undefined。我正在获得ERORR!作为回应 –

+0

req.body.Id等同于存储在Mongodb中的id(用于索引的id,24个字符长)? –

+0

没有。我正在使用自定义ID –