Update documents

Update documents

Update operators change selected fields; replacement changes the document body. Compare matchedCount and modifiedCount instead of assuming every matched document changed.

MongoDB allows you to update existing documents in a collection using methods like updateOne() and updateMany(). These updates can modify specific fields, add new ones, increment values, or remove fields — all without replacing the entire document.

Before working through the examples, make sure you can connect with mongosh and select your database. Use a practice database for writes and deletions.

  1. By the end of this lesson

    Use $set, $inc and $unset without accidentally replacing unrelated fields.

Step-by-Step: How Update Works

  1. Select the document(s) to update using a query filter
  2. Specify what to change using update operators like $set, $inc, $unset, etc.
  3. Use updateOne() to update a single matching document or updateMany() to update all matching documents

Example 1: Using $set to Update a Field

Let’s first insert a sample document:


    db.users.insertOne({
      name: "Alice",
      age: 25,
      city: "Delhi"
    });

Now we want to update Alice's city to "Mumbai":


    db.users.updateOne(
      { name: "Alice" },
      { $set: { city: "Mumbai" } }
    );
    {
      acknowledged: true,
      matchedCount: 1,
      modifiedCount: 1
    }

Explanation: The $set operator updates the city field to "Mumbai". If the field does not exist, it will be added.

Example 2: Updating Multiple Documents with updateMany()

Let’s insert more users:


    db.users.insertMany([
      { name: "Bob", age: 30, city: "Delhi" },
      { name: "Charlie", age: 28, city: "Delhi" }
    ]);

Now update everyone living in "Delhi" to "Bangalore":


    db.users.updateMany(
      { city: "Delhi" },
      { $set: { city: "Bangalore" } }
    );
    {
      acknowledged: true,
      matchedCount: 2,
      modifiedCount: 2
    }

Explanation: This updates all users where city is "Delhi" to now say "Bangalore".

Example 3: Using $inc to Increment a Numeric Field

Suppose you want to increase Bob's age by 1:


    db.users.updateOne(
      { name: "Bob" },
      { $inc: { age: 1 } }
    );
    {
      acknowledged: true,
      matchedCount: 1,
      modifiedCount: 1
    }

Explanation: The $inc operator adds the given value to the existing value of age.

Example 4: Using $unset to Remove a Field

Let’s remove the city field from Charlie’s document:


    db.users.updateOne(
      { name: "Charlie" },
      { $unset: { city: "" } }
    );
    {
      acknowledged: true,
      matchedCount: 1,
      modifiedCount: 1
    }

Explanation: The $unset operator deletes the specified field from the document. The value you provide doesn’t matter (commonly an empty string).

Intuition Check

Q: What happens if you use updateOne() without any operator like $set?

A: It will replace the entire document with the new one, which can accidentally delete other fields. Always use update operators when modifying specific fields.

Bonus: Replace Entire Document

To replace the whole document, you can do this:


    db.users.replaceOne(
      { name: "Alice" },
      { name: "Alice", age: 26 }
    );
    {
      acknowledged: true,
      matchedCount: 1,
      modifiedCount: 1
    }

Note: The previous city field will be removed since it's not included in the new object.

Check your understanding

A student has { name: "Maya", marks: 70 }. What does $inc: { marks: 5 } change?
Choose one answer. You can try again.

Answer and explanation

marks becomes 75 and name stays Maya.

$inc adds to the numeric field and preserves unrelated fields.

What to remember

Update operators change selected fields; replacement changes the document body. Compare matchedCount and modifiedCount instead of assuming every matched document changed.

Summary

  • updateOne() – updates the first document that matches the filter
  • updateMany() – updates all matching documents
  • $set – modifies or adds a field
  • $inc – increments or decrements a field value
  • $unset – removes a field

Understanding and using MongoDB's update operators is key to managing data efficiently. They allow partial updates without rewriting entire documents, saving performance and avoiding errors.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Delete documents →