Created
February 23, 2019 19:08
-
-
Save Bilguun132/644378a7a471db5c37a26b2e2caeb5e7 to your computer and use it in GitHub Desktop.
MongoDemo-Tutorial-Index.js with update and delete
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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