DEV Community

Rittwick Bhabak
Rittwick Bhabak

Posted on

#11 Updating Record

Various mongoose methods

  • char.update()
  • MarioChar.update()
  • MarioChar.findOneAndUpdate()
// Saving the data
character = new MarioChar({
    name: "Mario",
    weight: 60
})
character.save().then(function(){
    ...
})

// Updating the data
MarioChar.findOneAndUpdate({ name:"Mario"}, { name:"new_name"}).then(function(){
    MarioChar.findOne({ name:"Mario" }).then(result => {
        ...
    })
})
Enter fullscreen mode Exit fullscreen mode

The the new changes will be injected and this will not overwrite the complete objects. This update results to the object:
{ name: "new_name", weight: 100 } and not just { name: "new_name" }

Top comments (0)