Assignments for Day 7
MongoDB CRUD Operations – Part 3 (Update)
Assignment 1: Update a Single Document Using updateOne()
Problem:
You have a students collection. Update the grade of the student named "John" to "A+".
Solution:
Use the updateOne() method to update the grade of the student named "John" to "A+".
db.students.updateOne(
{ name: "John" }, // filter: match student with name "John"
{ $set: { grade: "A+" } } // update: set grade to "A+"
)
Explanation:
- The updateOne() method updates the first document that matches the filter. In this case, it will match the document where the name is "John" and set the grade field to "A+".
Assignment 2: Update Multiple Documents Using updateMany()
Problem:
You have a students collection. Update the grade to "B" for all students who are in class "101".
Solution:
Use the updateMany() method to update the grade of all students in class "101".
db.students.updateMany(
{ class_id: 101 }, // filter: match students in class 101
{ $set: { grade: "B" } } // update: set grade to "B"
)
Explanation:
- The updateMany() method is used to update all documents that match the filter. In this case, all students in class "101" will have their grade updated to "B".
Assignment 3: Replace Document Using replaceOne()
Problem:
You have a students collection. Replace the entire document of the student with name "Alice" with a new document that includes the following information:
{ "name": "Alice", "grade": "A+", "class_id": 102, "age": 20 }
Solution:
Use the replaceOne() method to replace the entire document.
db.students.replaceOne(
{ name: "Alice" }, // filter: match student "Alice"
{ name: "Alice", grade: "A+", class_id: 102, age: 20 } // new document
)
Explanation:
- The replaceOne() method replaces the entire document that matches the filter. The new document will replace the old one entirely.
Assignment 4: Increment Field Using $inc Operator
Problem:
You have a students collection. Increment the age of the student with name "John" by 1 year.
Solution:
Use the $inc operator to increment the age by 1.
db.students.updateOne(
{ name: "John" }, // filter: match student "John"
{ $inc: { age: 1 } } // increment age by 1
)
Explanation:
- The $inc operator is used to increment a field's value. In this case, the age field of the student named "John" is incremented by 1.
Assignment 5: Add a New Element to an Array Using $push Operator
Problem:
You have a students collection. Add a new course "History" to the courses array for the student with name "Charlie".
Solution:
Use the $push operator to add "History" to the courses array.
db.students.updateOne(
{ name: "Charlie" }, // filter: match student "Charlie"
{ $push: { courses: "History" } } // push "History" into courses array
)
Explanation:
- The $push operator is used to append a value to an array. Here, the course "History" is added to the courses array for the student "Charlie".
Assignment 6: Remove an Element from an Array Using $pull Operator
Problem:
You have a students collection. Remove the course "Math" from the courses array for the student named "Alice".
Solution:
Use the $pull operator to remove "Math" from the courses array.
db.students.updateOne(
{ name: "Alice" }, // filter: match student "Alice"
{ $pull: { courses: "Math" } } // pull "Math" from courses array
)
Explanation:
- The $pull operator is used to remove an element from an array that matches the specified value. Here, "Math" is removed from the courses array of the student "Alice".
Assignment 7: Update a Document Only If It Exists Using upsert
Problem:
You have a students collection. Try to update the grade of the student named "John" to "A" if he exists. If not, insert a new student with name "John" and grade "A".
Solution:
Use the upsert option with updateOne() to either update or insert the document.
db.students.updateOne(
{ name: "John" }, // filter: match student "John"
{ $set: { grade: "A" } }, // update: set grade to "A"
{ upsert: true } // upsert: insert if not found
)
Explanation:
- The upsert option ensures that if the document doesn't exist, MongoDB will insert a new document instead of doing nothing.
Assignment 8: Rename a Field Using $rename Operator
Problem:
You have a students collection. Rename the grade field to marks for the student named "John".
Solution:
Use the $rename operator to rename the field.
db.students.updateOne(
{ name: "John" }, // filter: match student "John"
{ $rename: { "grade": "marks" } } // rename "grade" to "marks"
)
Explanation:
- The $rename operator is used to rename a field in the document. Here, the grade field is renamed to marks for the student named "John".
Assignment 9: Remove a Field from a Document Using $unset
Problem:
You have a students collection. Remove the age field for the student named "Alice".
Solution:
Use the $unset operator to remove the field.
db.students.updateOne(
{ name: "Alice" }, // filter: match student "Alice"
{ $unset: { age: "" } } // unset: remove the "age" field
)
Explanation:
- The $unset operator is used to remove a field from a document. Here, the age field is removed from the student "Alice".
Assignment 10: Update Documents Based on Condition Using $set and $inc
Problem:
You have a students collection. If a student’s grade is "B", increase their age by 1 year. If a student’s grade is "A", set their grade to "A+".
Solution:
Use conditional updates with $set and $inc.
db.students.updateMany(
{ grade: "B" }, // filter: match students with grade "B"
{ $inc: { age: 1 } } // increment age by 1
)
db.students.updateMany(
{ grade: "A" }, // filter: match students with grade "A"
{ $set: { grade: "A+" } } // update grade to "A+"
)
Explanation:
- The first updateMany() call increments the age field by 1 for students with grade "B".
- The second updateMany() call sets the grade field to "A+" for students with grade "A".
