MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Update Operations: updateOne, updateMany & Field Modifiers ($set, $inc, $push)0%
Study Materials
MongoDB & MongooseProgress 0%
1.1Document Databases vs Relational SQL: CAP Theorem & When to Use NoSQL1.2JSON vs BSON Data Types & MongoDB Internal Storage Engine (WiredTiger)1.3MongoDB Atlas Cloud Setup, Network Whitelisting & Compass GUI
2.1Insert Operations: insertOne, insertMany & Write Concerns2.2Find Queries & Comparison Operators ($eq, $gt, $in, $nin)2.3Logical, Element & Array Query Operators ($and, $or, $exists, $all)2.4Update Operations: updateOne, updateMany & Field Modifiers ($set, $inc, $push)2.5Delete Operations, Cursors, Limit, Skip & Sorting in MongoDB
HomeTutorialsMongoDB & MongooseUpdate Operations: updateOne, updateMany & Field Modifiers ($set, $inc, $push)
Delete Operations, Cursors, Limit, Skip & Sorting in MongoDB

Update Operations: updateOne, updateMany & Field Modifiers ($set, $inc, $push)

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

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.
1// Update a single user's status
2const result = await db.collection('users').updateOne(
3 { _id: new ObjectId('64fa123456789abcdef01234') },
4 { $set: { status: 'ACTIVE', lastLogin: new Date() } }
5);
6 
7console.log('Matched:', result.matchedCount, 'Modified:', result.modifiedCount);

2. Essential Field Update Modifiers

OperatorActionExample
$setSets or creates the value of a field{ $set: { price: 2999, 'meta.verified': true } }
$unsetDeletes a field from the document{ $unset: { temporaryToken: "" } }
$incIncrements/decrements a numeric field atomically{ $inc: { views: 1, inventory: -1 } }
$min / $maxUpdates only if the value is less/greater than current{ $max: { highScore: 980 } }
$currentDateSets 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:
1await db.collection('courses').updateOne(
2 { slug: 'full-stack-mern-mastery' },
3 { $push: { tags: 'docker' } }
4);
  • $addToSet: Adds an item only if it does not already exist in the array (prevents duplicate tags):
1await db.collection('courses').updateOne(
2 { slug: 'full-stack-mern-mastery' },
3 { $addToSet: { tags: 'react' } } // Will not duplicate if 'react' is already present!
4);
  • $pull: Removes all occurrences of matching items from an array:
1await db.collection('courses').updateOne(
2 { slug: 'full-stack-mern-mastery' },
3 { $pull: { tags: 'deprecated-tech' } }
4);

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:

1await db.collection('analytics').updateOne(
2 { pageUrl: '/courses/mern', date: '2026-09-15' },
3 { $inc: { views: 1 } },
4 { upsert: true } // Creates record on 1st visit, increments on subsequent visits!
5);

Interactive Quiz• Knowledge Check

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

Previous LessonNext Lesson
Logical, Element & Array Query Operators ($and, $or, $exists, $all)Delete Operations, Cursors, Limit, Skip & Sorting in MongoDB

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master MongoDB & Mongoose with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext