updateOne(filter, update)changes the first match;updateManychanges all matches.$setsets fields,$unsetremoves them,$incadds to numbers.$push/$pulladd or remove array items.{ upsert: true }inserts the document if none matches.
MongoDB Update
updateOne, updateMany and update operators.
✍️ 2 exercises in this lessonKey Takeaways: MongoDB Update
Mastering MongoDB Update helps you write clean and reliable MongoDB code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.
MongoDB Update Code Example
MongoDBdb.students.updateOne({ name: "Usman" }, { $set: { city: "Rawalpindi" }, $inc: { marks: 5 } })
db.students.updateMany({}, { $set: { course: "MongoDB Basics" } })
Output
{ acknowledged: true, matchedCount: 1, modifiedCount: 1 }
{ acknowledged: true, matchedCount: 3, modifiedCount: 3 }
Run this example on your own computer. The "Get Started" lesson shows how to set it up.
MongoDB Update Practice Exercises
Check what you learned in “MongoDB Update”.
/ 2 solved
Fill in the blank
Set a field to a new value.
db.students.updateOne({ name: "Ali" }, { : { city: "Multan" } })
✓ Correct!
Not quite. Try again.
Multiple choice
Which operator adds a number to a field?
✓ Correct!
Not quite. Try again.
to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.
Stuck on something?
Ask the community in the CodesInTech forum.
Need a developer?
Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.
Explore Our Services