learnNcode
learnNcode

Reputation: 149

How can i modify a field of a record by specifying one of its matching field in mongodb using nodejs

I have a studentRollNumber with me. And I wish to modify a field in student record with that particular roll number. How can I do this. I tried the below code. I would like to fetch the record with the studentnumber 123 and modify its status field to 0

function updateRecord(){
   var studentNumber = '123';
   var filter = { studentNum: studentNumber };
   var newStatus = Number(0);
   var modifyStatus ={status : newStatus}

   Student.findOneAndUpdate(filter,modifyStatus,(err)=>{
     if(!err){
       console.log('updation to db sucess')  
      }
 })

Upvotes: 0

Views: 33

Answers (1)

Saiem Saeed
Saiem Saeed

Reputation: 57

You can do something like following,

function updateRecord(){
 var studentNumber = '123';
 var filter = { studentNum: studentNumber };
 var newStatus = Number(0);
 var modifyStatus = {$set:{status:newStatus}}

 Student.findOneAndUpdate(filter , modifyStatus, {new: true})
 .then(() => console.log('Record Update Successfully');

}

Upvotes: 1

Related Questions