MongoDB Tutorial 0/120 lessons ~6 min read Lesson 34
Update Documents
Updates in MongoDB are atomic per document and use update operators rather than rewriting whole documents.
Course progress0%
Focus
4 guided sections
Practice signal
Examples included
Career prep
Foundation builder
Introduction
Updates in MongoDB are atomic per document and use update operators rather than rewriting whole documents. $set, $inc, $push, $unset — each does one job, surgically.
Syntax reference
js
// Set + incrementdb.users.updateOne({ _id: 1 },{ $set: { lastLogin: new Date() }, $inc: { loginCount: 1 } });// Manydb.products.updateMany({ stock: { $lt: 5 } }, { $set: { lowStock: true } });// Upsertdb.counters.updateOne({ _id: "orders" }, { $inc: { seq: 1 } }, { upsert: true });
Real-world use
Every 'increment likes' or 'mark read' button in a UI maps to one update operator.
Best practices
- Use
$incfor counters — atomic and safe under concurrency. - Use
upsert: truefor 'insert if missing'.
Ready to mark this lesson complete?Track your journey across the entire course.