Upsert and Replace
With upsert: true, update inserts a document when nothing matches. replaceOne swaps the whole document except _id.
Example
db.counters.updateOne({ _id: "visits" }, { $inc: { n: 1 } }, { upsert: true })
db.users.replaceOne({ _id: 1 }, { name: "New", age: 1 })Exercise
Insert if missing.
{ : true }
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.