Database IntegrationLesson 68Free preview
MongoDB Update
This lesson is part of Node.js · All levels
Summary
This lesson teaches updating MongoDB documents from Node.js with updateOne, updateMany, and replaceOne. You use operators like $set, $inc, and $push to change some or all of the contents of a document without losing any other fields. The results of the operation loaded matchedCount and modifiedCount to verify the changes.
Key points
- $set specifies the field that has been changed; $unset deletes the field; $inc adds numbers.
- UpdateOne changes one document, updateMany changes all that matches.
- Upsert option: true create documents when they do not exist (update-or-create pattern).
- ReplaceOne replaces all the contents of the document except _id; updateOne changes some.
- The results loaded matchedCount (which matches) and modifiedCount (which actually changes).
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.