Skip to content

Instantly share code, notes, and snippets.

@Bilguun132
Created February 23, 2019 19:08
Show Gist options
  • Select an option

  • Save Bilguun132/644378a7a471db5c37a26b2e2caeb5e7 to your computer and use it in GitHub Desktop.

Select an option

Save Bilguun132/644378a7a471db5c37a26b2e2caeb5e7 to your computer and use it in GitHub Desktop.
MongoDemo-Tutorial-Index.js with update and delete
async function updateMovie(id) {
//query first
const movie = await Movie.findById(id);
if (!movie) return;
movie.name = 'New Movie'; //or you can do movie.set({name:'New Movie'})
movie.save();
//update first
const result = await Movie.update({_id: id}, {
$set: {name: 'New Movie'}
})
//update document then return
const result = await Movie.findOneAndUpdate({_id: id}, {
$set: {name: 'New Movie'}
}, {new: true}) //returns updated document
}
async function deleteMovie(id) {
const result = await Movie.deleteOne({_id: id});
const result = await Movie.deleteMany({_id: id});
const result = await Movie.findByIdAndDelete({_id: id})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment