Update and Delete Operations · MongoDB
Ch 2 · MongoDB CRUD Operations — card 5 of 8.
updateOne, updateMany, deleteOne, and deleteMany modify or remove documents. Each takes a filter first to choose which documents are affected. For updates, a second argument uses operators such as $set to change a field, $inc to add to a number, $unset to remove a field and $push to append to an array. Take care with delete filters: an empty filter matches every document.
Update operators: $set changes fields, $inc increments numbers, $unset removes fields, $push adds to arrays. The first argument is the filter, second is the update.
- $set replaces field values
- $inc adds to numeric fields
- $push appends to arrays
- deleteMany removes all matches