Nearby lessons

7 of 22

⚙️ MongoDB: Update and Array Operators

MongoDB provides powerful update operators to modify documents and arrays within a collection. You can add, remove, rename, or manipulate fields and arrays dynamically.

📚 Initial Data Setup

MongoDB Shell
NoSQL
Database
1db.students.insertMany([
2 { name: 'Akshay Kumar', age: 23, class: 'BCA', skills: ['C++', 'Python'] },
3 { name: 'Rajesh Khanna', age: 24, class: 'Btech', skills: ['HTML', 'PHP'] },
4 { name: 'Shahid Kapoor', age: 20, class: 'BSc', skills: ['HTML', 'Python', 'JavaScript'] },
5 { name: 'John Abraham', age: 19, class: 'BCA', skills: ['JavaScript', 'HTML', 'ASP.net'] },
6 { name: 'Aamir Khan', age: 24, class: 'Btech', skills: ['JavaScript', 'HTML'] }
7]);
Query Result
JSON Output
✅ 5 student documents inserted successfully.

1️⃣ Using $push — Add an Element to an Array

MongoDB Shell
NoSQL
Database
1db.students.updateOne(
2 { name: "Shahid Kapoor" },
3 { $push: { skills: "C++" } }
4);
Query Result
JSON Output
"skills": ["HTML", "Python", "JavaScript", "C++"]

2️⃣ Using $pop — Remove First or Last Element

MongoDB Shell
NoSQL
Database
1db.students.updateOne(
2 { name: "John Abraham" },
3 { $pop: { skills: 1 } } // 1 removes last, -1 removes first
4);
Query Result
JSON Output
"skills": ["JavaScript", "HTML"]

3️⃣ Using $pull — Remove a Specific Element

MongoDB Shell
NoSQL
Database
1db.students.updateOne(
2 { name: "Shahid Kapoor" },
3 { $pull: { skills: "Python" } }
4);
Query Result
JSON Output
"skills": ["HTML", "JavaScript"]

4️⃣ Using $addToSet — Add Unique Element

MongoDB Shell
NoSQL
Database
1db.students.updateOne(
2 { name: "Aamir Khan" },
3 { $addToSet: { skills: "PHP" } }
4);
Query Result
JSON Output
"skills": ["JavaScript", "HTML", "PHP"]

5️⃣ Using $set — Update a Field

MongoDB Shell
NoSQL
Database
1db.students.updateOne(
2 { name: "Akshay Kumar" },
3 { $set: { class: "MCA" } }
4);
Query Result
JSON Output
"class": "MCA"

6️⃣ Using $unset — Remove a Field

MongoDB Shell
NoSQL
Database
1db.students.updateOne(
2 { name: "Rajesh Khanna" },
3 { $unset: { skills: "" } }
4);
Query Result
JSON Output
❌ Field 'skills' has been removed from the document.

7️⃣ Using $rename — Rename a Field

MongoDB Shell
NoSQL
Database
1db.students.updateOne(
2 { name: "Shahid Kapoor" },
3 { $rename: { class: "course" } }
4);
Query Result
JSON Output
"course": "BSc"

8️⃣ Using $inc — Increment a Field Value

MongoDB Shell
NoSQL
Database
1db.students.updateOne(
2 { name: "John Abraham" },
3 { $inc: { age: 1 } }
4);
Query Result
JSON Output
"age": 20

9️⃣ Using $mul — Multiply a Field Value

MongoDB Shell
NoSQL
Database
1db.students.updateOne(
2 { name: "Aamir Khan" },
3 { $mul: { age: 2 } }
4);
Query Result
JSON Output
"age": 48

🔟 Using $currentDate — Add/Update Date Field

MongoDB Shell
NoSQL
Database
1db.students.updateOne(
2 { name: "Akshay Kumar" },
3 { $currentDate: { updated_at: true } }
4);
Query Result
JSON Output
"updated_at": ISODate("YYYY-MM-DDTHH:MM:SSZ")

1️⃣1️⃣ Using replaceOne — Replace Entire Document

MongoDB Shell
NoSQL
Database
1// Before replacing:
2{
3 "_id": ObjectId("603d9b58b4c79e1b8c4b1234"),
4 "name": "Akshay Kumar",
5 "class": "BCA",
6 "age": 22
7}
8
9// Replace document:
10db.students.replaceOne(
11 { name: "Akshay Kumar" },
12 { name: "Akshay Kumar", course: "MCA", year: 2025 }
13);
14
15// After replacing:
16{
17 "_id": ObjectId("603d9b58b4c79e1b8c4b1234"),
18 "name": "Akshay Kumar",
19 "course": "MCA",
20 "year": 2025
21 }
Query Result
JSON Output
✅ Document replaced — old fields ('class', 'age') removed.
Keep Going!MongoDB - Delete Document