Updating Documents

Updating Documents: Changing Documents with updateOne and $set, More Update Operators: $inc, $push, $pull and $addToSet, Upserts and Replacing a Whole Document.

Updated

  1. Changing Documents with updateOne and $set (Apprentice): updateOne and updateMany change documents where they are, with a filter that picks the documents and an update operator that says what changes; $set writes fields, $unset removes them, and matchedCount and modifiedCount report what happened.
  2. More Update Operators: $inc, $push, $pull and $addToSet (Apprentice): $inc lets the database do the counting so no count is lost, $currentDate stamps when a change happened, $push and $pull add to and remove from a list, and $addToSet adds an item only when it isn't there yet.
  3. Upserts and Replacing a Whole Document (Journeyman): An upsert updates a document when the filter finds one and inserts it when it doesn't, $setOnInsert sets values only on that first insert, replaceOne swaps in a whole new version, and findOneAndUpdate changes a document and hands it back.