Update Operations: updateOne, updateMany & Field Modifiers ($set, $inc, $push)0%
Update Operations: updateOne, updateMany & Field Modifiers ($set, $inc, $push)
Beginner12 min read•Updated: Jul 11, 2026
Update Operations: updateOne, updateMany & Field Modifiers ($set, $inc, $push)
In MongoDB, modifying documents requires atomic update operators. Replacing an entire document accidentally is an infamous beginner mistake. MongoDB provides precise atomic field modifiers like $set, $inc, $push, and $pull to update documents safely.
1. Updating Documents: updateOne() vs updateMany()
updateOne(filter, update, options): Updates the first matching document.updateMany(filter, update, options): Updates all documents matching the filter.
JavaScript
// Update a single user's status
const result = await db.collection('users').updateOne(
{ _id: new ObjectId('64fa123456789abcdef01234') },
{ $set: { status: 'ACTIVE', lastLogin: new Date() } }
);
console.log('Matched:', result.matchedCount, 'Modified:', result.modifiedCount);
2. Essential Field Update Modifiers
| Operator | Action | Example |
|---|---|---|
$set | Sets or creates the value of a field | { $set: { price: 2999, 'meta.verified': true } } |
$unset | Deletes a field from the document | { $unset: { temporaryToken: "" } } |
$inc | Increments/decrements a numeric field atomically | { $inc: { views: 1, inventory: -1 } } |
$min / $max | Updates only if the value is less/greater than current | { $max: { highScore: 980 } } |
$currentDate | Sets field to current timestamp or Date | { $currentDate: { updatedAt: true } } |
3. Array Modifiers: $push, $pull, $addToSet
MongoDB allows modifying array fields directly inside a document without fetching the document into JavaScript memory:
$push: Appends a new item to an array:
JavaScript
await db.collection('courses').updateOne(
{ slug: 'full-stack-mern-mastery' },
{ $push: { tags: 'docker' } }
);
$addToSet: Adds an item only if it does not already exist in the array (prevents duplicate tags):
JavaScript
await db.collection('courses').updateOne(
{ slug: 'full-stack-mern-mastery' },
{ $addToSet: { tags: 'react' } } // Will not duplicate if 'react' is already present!
);
$pull: Removes all occurrences of matching items from an array:
JavaScript
await db.collection('courses').updateOne(
{ slug: 'full-stack-mern-mastery' },
{ $pull: { tags: 'deprecated-tech' } }
);
4. Upserts (upsert: true)
An Upsert is a hybrid operation: if a matching document is found, it is updated; if no document matches the filter, a new document is inserted:
JavaScript
await db.collection('analytics').updateOne(
{ pageUrl: '/courses/mern', date: '2026-09-15' },
{ $inc: { views: 1 } },
{ upsert: true } // Creates record on 1st visit, increments on subsequent visits!
);
Interactive Quiz
Multiple Choice Questions
Question 1
What happens if you accidentally pass
{ price: 1999 } without $set as the update parameter in collection.updateOne()?Question 2
Which operator increments a numerical field atomically, eliminating race conditions during high-concurrency balance or view counter updates?
Question 3
How does
$addToSet differ from $push when modifying an array field?Question 4
What does the
{ upsert: true } option accomplish in an update operation?Question 5
Which operator completely removes a specified field from a document?
Next Lesson
Delete Operations, Cursors, Limit, Skip & Sorting in MongoDB
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.