In MongoDB, we use update methods to modify existing documents inside a collection. For example, we can change a student's name, update a fee, increase a salary, or add a new field.
Updating means changing one or more values in an existing document without necessarily replacing the entire document.
Consider this document in the students collection:
{
_id: 1,
name: "Rahul",
age: 20,
course: "Java",
fee: 5000
}
Suppose Rahul changes his course from Java to Python.
We can update the course field.
The updateOne() method updates the first
document that matches the specified filter.
db.collection.updateOne(
{ filter },
{ update }
)
db.students.updateOne(
{ _id: 1 },
{ $set: { course: "Python" } }
)
Explanation:
students is the collection name.{ _id: 1 } identifies the document.$set changes the specified field.course: "Python" sets the new course.Verify the updated document:
db.students.findOne({ _id: 1 })
The $set operator sets a field to a new
value. If the field does not exist, MongoDB adds it.
db.students.updateOne(
{ _id: 1 },
{ $set: { name: "Amit Kumar" } }
)
db.students.updateOne(
{ _id: 1 },
{
$set: {
name: "Amit Kumar",
age: 21,
course: "JavaScript"
}
}
)
Only the specified fields are changed. Other fields in the document remain unchanged.
The updateMany() method updates every
document that matches the filter.
db.students.updateMany(
{ course: "Java" },
{ $set: { course: "Java Full Stack" } }
)
All documents whose course is Java will
have their course changed to Java Full Stack.
db.students.updateMany(
{ status: "active" },
{ $set: { verified: true } }
)
This adds or updates the verified field
for every active student.
The $inc operator increases or decreases
a numeric field by the specified amount.
db.students.updateOne(
{ _id: 1 },
{ $inc: { fee: 500 } }
)
If the student's fee was 5000, it becomes 5500.
db.students.updateOne(
{ _id: 1 },
{ $inc: { fee: -200 } }
)
If the current fee is 5500, it becomes 5300.
The $unset operator removes a field
from a document.
db.students.updateOne(
{ _id: 1 },
{ $unset: { age: "" } }
)
The age field is removed from the
matching document. The other fields remain unchanged.
The $rename operator changes a field's
name.
db.students.updateOne(
{ _id: 1 },
{ $rename: { name: "student_name" } }
)
The field name is renamed to
student_name in the matching document.
The upsert option inserts a new document
if no document matches the filter. If a matching
document exists, MongoDB updates it.
db.students.updateOne(
{ _id: 100 },
{
$set: {
name: "Priya",
course: "Python"
}
},
{ upsert: true }
)
If a document with _id: 100 exists,
it is updated. Otherwise, MongoDB creates a new
document with that ID and the specified fields.
updateOne() updates at most one matching document.updateMany() updates all matching documents.$set adds or changes field values.$inc increases or decreases numeric values.$unset removes a field.$rename renames a field.upsert: true inserts a document when no match is found.
Warning: An update with an overly broad
filter may change many documents. Check the filter carefully,
especially when using updateMany().
Create a collection named practice_students
and insert these sample documents:
db.practice_students.insertMany([
{
_id: 1,
name: "Rahul",
age: 20,
course: "Java",
fee: 5000
},
{
_id: 2,
name: "Priya",
age: 21,
course: "Java",
fee: 6000
},
{
_id: 3,
name: "Aman",
age: 19,
course: "Python",
fee: 4500
}
])
status with value "active" to all students.name field to student_name for Aman.find().Run the insertion command only once, or clear the test collection before inserting the same sample data again.
1. Which method updates all matching documents?
2. Which operator increases a numeric value?
3. What does upsert: true do?