Here are different MongoDB update() examples in English, explained simply.

Note: In modern MongoDB, use updateOne() and updateMany() instead of the older update() method.

1. Update One Field

Suppose we have a students collection:

db.students.updateOne(
    { name: "Rahul" },
    { $set: { city: "Rajkot" } }
)

Meaning: Find Rahul and change his city to Rajkot.


2. Update Multiple Fields

db.students.updateOne(
    { name: "Rahul" },
    {
        $set: {
            city: "Rajkot",
            country: "India"
        }
    }
)

This updates both city and country.


3. Update Age

db.students.updateOne(
    { name: "Amit" },
    { $set: { age: 25 } }
)

If Amit’s age was 22, it becomes 25.


4. Update Multiple Records — updateMany()

For example, update the country of all students from Rajkot:

db.students.updateMany(
    { city: "Rajkot" },
    { $set: { country: "India" } }
)

This updates all documents where city is "Rajkot".


5. Increase a Number Using $inc

Increase Rahul’s age by 1:

db.students.updateOne(
    { name: "Rahul" },
    { $inc: { age: 1 } }
)

If age is 24, it becomes 25.

Increase by 10:

db.students.updateOne(
    { name: "Rahul" },
    { $inc: { age: 10 } }
)

6. Rename a Field Using $rename

Change field city to location:

db.students.updateOne(
    { name: "Rahul" },
    { $rename: { "city": "location" } }
)

7. Delete a Field Using $unset

Remove the country field:

db.students.updateOne(
    { name: "Rahul" },
    { $unset: { country: "" } }
)

8. Update Using a Condition

Make all students above 18 years old "Adult":

db.students.updateMany(
    { age: { $gt: 18 } },
    { $set: { status: "Adult" } }
)

Common comparison operators:

$gt   → greater than
$lt   → less than
$gte  → greater than or equal
$lte  → less than or equal

9. Old update() Method

The older syntax was:

db.students.update(
    { name: "Rahul" },
    { $set: { city: "Rajkot" } }
)

Modern MongoDB prefers:

db.students.updateOne(
    { name: "Rahul" },
    { $set: { city: "Rajkot" } }
)

Quick Summary

Method / OperatorPurpose
updateOne()Update one document
updateMany()Update multiple documents
$setAdd/change a value
$incIncrease/decrease a number
$renameRename a field
$unsetRemove a field

Basic syntax:

db.collection.updateOne(
    { condition },
    { $set: { field: value } }
)

Yes. In MongoDB, multi: true is used with the older update() method when you want to update multiple documents.

Example

Suppose your students collection contains:

{ name: "Rahul", city: "Rajkot", age: 20 }
{ name: "Amit", city: "Rajkot", age: 22 }
{ name: "Ravi", city: "Ahmedabad", age: 25 }

If you want to update all students from Rajkot:

db.students.update(
    { city: "Rajkot" },
    { $set: { country: "India" } },
    { multi: true }
)

multi: true vs multi: false

// Only first matching document
db.students.update(
    { city: "Rajkot" },
    { $set: { country: "India" } },
    { multi: false }
)
// All matching documents
db.students.update(
    { city: "Rajkot" },
    { $set: { country: "India" } },
    { multi: true }
)

Modern MongoDB equivalent

Instead of:

{ multi: true }

use:

db.students.updateMany(
    { city: "Rajkot" },
    { $set: { country: "India" } }
)

Easy rule:

update() + multi:true  → update multiple documents
updateOne()             → update one document
updateMany()            → update multiple documents

MongoDB Array Update Operators. A few lines need correction. Here is a clean, student-friendly version with the meaning of each operator.

1. Create database and collection

use stud

db.createCollection("s1")

db.s1.insertOne({
    no: 1,
    nm: "xyz",
    marks: [40, 50, 60]
})

2. $pop — Remove first or last array element

Remove last element:

db.s1.updateOne(
    { no: 1 },
    { $pop: { marks: 1 } }
)

Result:

marks: [40, 50]

Remove first element:

db.s1.updateOne(
    { no: 1 },
    { $pop: { marks: -1 } }
)

Result:

marks: [50, 60]

Remember:

$pop:  1  → last element
$pop: -1  → first element

3. $addToSet — Add value without duplicate

db.s1.updateOne(
    { no: 1 },
    { $addToSet: { marks: 55 } }
)

If:

marks: [40, 50, 60]

Result:

marks: [40, 50, 60, 55]

If you execute it again, 55 will not be added again.

$addToSet is useful when you don’t want duplicate values.


4. $pull — Remove matching values

db.s1.updateOne(
    { no: 1 },
    { $pull: { marks: { $in: [50, 60] } } }
)

If:

marks: [40, 50, 60, 55]

Result:

marks: [40, 55]

$in means match any value from the given list.


5. $pullAll — Remove multiple specific values

db.s1.updateOne(
    { no: 1 },
    { $pullAll: { marks: [40, 55] } }
)

Result:

marks: []

Difference:

$pull     → remove based on a condition
$pullAll  → remove multiple specified values

6. $push — Add an element to the array

db.s1.updateOne(
    { no: 1 },
    { $push: { marks: 60 } }
)

If:

marks: [40, 50]

Result:

marks: [40, 50, 60]

Unlike $addToSet, $push allows duplicates.


7. $push with $each and $position

Your example:

db.s1.updateOne(
    { no: 1 },
    {
        $push: {
            marks: {
                $each: [55],
                $position: 2
            }
        }
    }
)

Suppose:

marks: [40, 50, 60]

$position: 2 means insert at array index 2.

Result:

marks: [40, 50, 55, 60]

Remember that MongoDB array indexes start from 0:

Index:   0   1   2
         ↓   ↓   ↓
marks:  40  50  60

So position 2 means before 60.


8. $push into another array

First insert:

db.alpha.insertOne({
    id: 111,
    text: ["a", "b", "d", "e", "g"]
})

Your last query has a mistake:

❌ Incorrect:

db.alpha.insert({id:111},{$push:{text:"h"}})

Because insert() is for inserting a new document, not updating an existing document.

✅ Correct:

db.alpha.updateOne(
    { id: 111 },
    { $push: { text: "h" } }
)

Result:

{
    id: 111,
    text: ["a", "b", "d", "e", "g", "h"]
}

Quick Revision Table

OperatorPurposeExample
$popRemove first/last$pop:{marks:1}
$addToSetAdd without duplicate$addToSet:{marks:55}
$pullRemove matching elements$pull:{marks:50}
$pullAllRemove multiple values$pullAll:{marks:[40,50]}
$pushAdd element$push:{marks:60}
$eachAdd multiple elements$each:[55,65]
$positionInsert at specific index$position:2

Most important difference for students:

$push       → adds value, duplicates allowed
$addToSet   → adds value, duplicates NOT allowed
$pop        → removes first/last
$pull       → removes matching values
$pullAll    → removes multiple specified values

By admin