Introduction
Updating documents is an essential MongoDB skill because real applications constantly need to change existing data. MongoDB provides methods such as updateOne(), updateMany(), and replaceOne() for modifying documents. In this chapter, you will practice updating individual fields, multiple documents, numbers, arrays, nested fields, and complete documents using practical MongoDB queries. MongoDB Update Documents Practice questions with solutions to help you understand the concepts.
Q1. Update one document using updateOne()
Problem Statement
Update Rahul’s course from "Python" to "MongoDB".
MongoDB Command / Query
db.students.updateOne(
{ name: "Rahul" },
{
$set: {
course: "MongoDB"
}
}
)
Expected Output
{
acknowledged: true,
matchedCount: 1,
modifiedCount: 1
}
Explanation
updateOne() updates the first document that matches the filter.
The $set operator changes only the specified field:
$set: {
course: "MongoDB"
}
Other fields in Rahul’s document remain unchanged.
Q2. Update multiple fields in one document
Problem Statement
Update Rahul’s age to 16 and change his course to "Data Analytics".
MongoDB Command / Query
db.students.updateOne(
{ name: "Rahul" },
{
$set: {
age: 16,
course: "Data Analytics"
}
}
)
Expected Output
{
acknowledged: true,
matchedCount: 1,
modifiedCount: 1
}
Explanation
You can update multiple fields using a single $set.
Only age and course are changed. Other fields remain intact.
Q3. Increase a numeric field using $inc
Problem Statement
Increase Aman’s age by 1.
MongoDB Command / Query
db.students.updateOne(
{ name: "Aman" },
{
$inc: {
age: 1
}
}
)
Expected Output
{
acknowledged: true,
matchedCount: 1,
modifiedCount: 1
}
Explanation
$inc is used to increase or decrease a numeric value.
For example:
$inc: {
age: 1
}
increases the age by 1.
To decrease it, you could use:
$inc: {
age: -1
}
Q4. Update multiple documents using updateMany()
Problem Statement
Change the course of all students currently enrolled in "Python" to "Advanced Python".
MongoDB Command / Query
db.students.updateMany(
{ course: "Python" },
{
$set: {
course: "Advanced Python"
}
}
)
Expected Output
{
acknowledged: true,
matchedCount: 3,
modifiedCount: 3
}
Explanation
updateMany() updates all documents that match the filter.
If three students have course: "Python", all three will be updated.
The actual matchedCount and modifiedCount depend on your collection.
Q5. Add a new field using $set
Problem Statement
Add an active field with the value true to the student named Neha.
MongoDB Command / Query
db.students.updateOne(
{ name: "Neha" },
{
$set: {
active: true
}
}
)
Expected Output
{
acknowledged: true,
matchedCount: 1,
modifiedCount: 1
}
Explanation
$set can update an existing field or create the field if it does not already exist.
The document will now contain:
{
name: "Neha",
active: true
}
along with its other existing fields.
Q6. Remove a field using $unset
Problem Statement
Remove the phone field from Rahul’s document.
MongoDB Command / Query
db.students.updateOne(
{ name: "Rahul" },
{
$unset: {
phone: ""
}
}
)
Expected Output
{
acknowledged: true,
matchedCount: 1,
modifiedCount: 1
}
Explanation
$unset removes a field from a document.
The value assigned to the $unset field is not important. An empty string is commonly used:
$unset: {
phone: ""
}
Q7. Add an item to an array using $push
Problem Statement
Add "React" to Neha’s skills array.
MongoDB Command / Query
db.students.updateOne(
{ name: "Neha" },
{
$push: {
skills: "React"
}
}
)
Expected Output
{
acknowledged: true,
matchedCount: 1,
modifiedCount: 1
}
Explanation
$push adds a new value to an array.
For example, if the original array is:
skills: ["Python", "MongoDB"]
after the update it becomes:
skills: ["Python", "MongoDB", "React"]
Q8. Remove an item from an array using $pull
Problem Statement
Remove "HTML" from Priya’s skills array.
MongoDB Command / Query
db.students.updateOne(
{ name: "Priya" },
{
$pull: {
skills: "HTML"
}
}
)
Expected Output
{
acknowledged: true,
matchedCount: 1,
modifiedCount: 1
}
Explanation
$pull removes array elements that match the specified value.
If the original array is:
skills: ["HTML", "CSS", "JavaScript"]
it becomes:
skills: ["CSS", "JavaScript"]
Q9. Update a nested field
Problem Statement
Change the city inside Rahul’s address document from "Delhi" to "Noida".
MongoDB Command / Query
db.students.updateOne(
{ name: "Rahul" },
{
$set: {
"address.city": "Noida"
}
}
)
Expected Output
{
acknowledged: true,
matchedCount: 1,
modifiedCount: 1
}
Explanation
Dot notation is used to update fields inside nested documents.
"address.city": "Noida"
means:
address
└── city → Noida
Other fields inside address, such as pincode, are not changed.
Q10. Replace an entire document using replaceOne()
Problem Statement
Replace Aman’s complete student document with a new document containing a name, age, and course.
MongoDB Command / Query
db.students.replaceOne(
{ name: "Aman" },
{
name: "Aman",
age: 18,
course: "Full Stack Development"
}
)
Expected Output
{
acknowledged: true,
matchedCount: 1,
modifiedCount: 1
}
Explanation
replaceOne() replaces the entire matching document, except for the _id value, which remains the same when the replacement document does not specify a different _id.
For example, if the old document was:
{
_id: ObjectId("..."),
name: "Aman",
age: 17,
course: "MongoDB",
skills: ["Python", "SQL"],
active: true
}
after replaceOne(), it becomes approximately:
{
_id: ObjectId("..."),
name: "Aman",
age: 18,
course: "Full Stack Development"
}
Fields such as skills and active are removed because the complete document was replaced.
Key Takeaways
updateOne()updates one matching document.updateMany()updates all documents matching a filter.replaceOne()replaces an entire document.$setchanges an existing field or creates a new field.$incincreases or decreases numeric values.$unsetremoves a field.$pushadds a value to an array.$pullremoves matching values from an array.- Dot notation can update fields inside nested documents.
matchedCounttells you how many documents matched the filter.modifiedCounttells you how many documents were actually modified.- Use
replaceOne()carefully because fields not included in the replacement document are removed.
FAQs
1. What is the updateOne() method in MongoDB?
updateOne() updates the first document that matches the specified filter.
db.students.updateOne(
{ name: "Rahul" },
{ $set: { age: 16 } }
)
2. What is the difference between updateOne() and updateMany()?
updateOne() modifies one matching document, while updateMany() modifies all documents that match the filter.
3. What does the $set operator do?
$set changes the value of a field or creates the field if it does not already exist.
{
$set: {
course: "MongoDB"
}
}
4. How can I increase a number in MongoDB?
Use the $inc operator:
{
$inc: {
age: 1
}
}
This increases the value of age by 1.
5. How can I remove a field from a MongoDB document?
Use $unset:
{
$unset: {
phone: ""
}
}
This removes the phone field.
6. How do I add a value to an array in MongoDB?
Use $push:
{
$push: {
skills: "React"
}
}
This adds "React" to the skills array.
7. What is the difference between updateOne() and replaceOne()?
updateOne() changes only the fields specified by update operators such as $set or $inc. replaceOne() replaces the complete matching document with a new document.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
