DEV Community

loading...

#11 Updating Record

rittwickbhabak profile image Rittwick Bhabak ・1 min read

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" }

Discussion (0)

pic
Editor guide