Insert the document if no match exists.
Section: Update and delete
Upsert a document
javascript
javascript
db.products.updateOne({ sku: "A103" }, { $set: { name: "Desk Lamp", price: 39 } }, { upsert: true })Explanation
Upserts are handy for idempotent seed and sync scripts.
Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing Update and delete