Lesson 16 of 50 – Updating Documents in MongoDB
32%

Updating Documents in MongoDB

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.

1. What Is Updating a Document?

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.

2. The updateOne() Method

The updateOne() method updates the first document that matches the specified filter.

Syntax
db.collection.updateOne(
    { filter },
    { update }
)
Example
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 })

3. The $set Operator

The $set operator sets a field to a new value. If the field does not exist, MongoDB adds it.

Change a Student's Name
db.students.updateOne(
    { _id: 1 },
    { $set: { name: "Amit Kumar" } }
)
Update Multiple Fields
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.

4. The updateMany() Method

The updateMany() method updates every document that matches the filter.

Example: Update All Students in a Course
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.

Example: Increase the Status of All Matching Records
db.students.updateMany(
    { status: "active" },
    { $set: { verified: true } }
)

This adds or updates the verified field for every active student.

5. The $inc Operator

The $inc operator increases or decreases a numeric field by the specified amount.

Example: Increase a Student's Fee
db.students.updateOne(
    { _id: 1 },
    { $inc: { fee: 500 } }
)

If the student's fee was 5000, it becomes 5500.

Example: Decrease a Fee
db.students.updateOne(
    { _id: 1 },
    { $inc: { fee: -200 } }
)

If the current fee is 5500, it becomes 5300.

6. The $unset Operator

The $unset operator removes a field from a document.

Example
db.students.updateOne(
    { _id: 1 },
    { $unset: { age: "" } }
)

The age field is removed from the matching document. The other fields remain unchanged.

7. The $rename Operator

The $rename operator changes a field's name.

Example
db.students.updateOne(
    { _id: 1 },
    { $rename: { name: "student_name" } }
)

The field name is renamed to student_name in the matching document.

8. Using upsert

The upsert option inserts a new document if no document matches the filter. If a matching document exists, MongoDB updates it.

Example
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.

9. Important Points to Remember

  • 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.
  • Always check your filter before running an update.

Warning: An update with an overly broad filter may change many documents. Check the filter carefully, especially when using updateMany().

10. Practical Assignment

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
    }
])
Complete these tasks:
  1. Change Rahul's course to Python.
  2. Change Priya's age to 22.
  3. Increase Aman's fee by 500.
  4. Change the course of all Java students to Java Full Stack.
  5. Add a field named status with value "active" to all students.
  6. Remove the age field from Rahul's document.
  7. Rename the name field to student_name for Aman.
  8. Display all updated documents using find().

Run the insertion command only once, or clear the test collection before inserting the same sample data again.

🧠 Quick Quiz

1. Which method updates all matching documents?




2. Which operator increases a numeric value?




3. What does upsert: true do?