Stage 2 · Lesson 6 of 9
updateOne, updateMany and update operators
updateOne(filter, update) changes the first matching document and updateMany changes all of them. The update uses operators: $set sets , $unset removes them, $inc adds to a number, $mul multiplies, $min and $max only change the value if the new one is lower or higher, $rename renames a field and $currentDate stamps the time.
Each operator changes only the named fields, and a change to one document is atomic (all or nothing), even if other clients write at the same moment. That is why $inc on a stock count is safe where read, add and write back is not. The result tells you matchedCount and modifiedCount; check them.
Common trapan updateMany with an empty or wrong filter, which changes every document in the collection.
A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock
Resources for this stage
Start with
- CourseFreeMongoDB University
MongoDB
Free: the CRUD lessons on inserting, finding, replacing, updating and deleting documents, with labs