Skip to content

MongoDB Update Multiple Documents AQSA Spark By {Examples}

  • by

Multiple documents can be updated simultaneously using the MongoDB updateMany() method. When we need to update a lot of documents in a collection, this technique comes in handy.

In this article, we will discuss a few examples of updating multiple documents in MongoDB with a single command. Let’s consider we have the collection student which is embedded with the following documents, I will update many of these documents by using updateMany() method.

# Create collection
db.student.insertMany([
{
_id: 1,
name: “Candice”,
age: 22,
marks: 99,
course: “Python”,
attendance: “Absent”
},
{
_id: 2,
name: “Jimmy”,
age: 23,
marks: 89,
course: “MongoDB”,
attendance: “Present”
},
{
_id: 3,
name: “Olive”,
age: 24,
marks: 79,
course: “C++”,
attendance: “Absent”
}
])

1. Update a Single Field in All Documents

Use the upadateMany() method to update a single field of all the documents using the upadateMany() method. For example, we called the updateMany() method below over the student collection. The first argument is kept empty which indicates that the update operation will apply to all the documents.

Then, we have specified the $set operator inside the second argument which will set the expression specified to it. The expression contains the course field which will be set with the value MongoDB for all the documents inside the collection.

# Updating single field in multiple documents
db.student.updateMany(
{},
{ $set: { course: “MongoDB” } }
)

The output indicates that all the documents are matched and modified with the value set to the specified field. 

By querying with the find() method, we can see the updated documents.

2. Update Multiple Fields in Multiple Documents

Next, we are using the updateMany() method to update the multiple fields in multiple documents.

Consider the following example where the updateMany() method is used over the collection student to update its documents. Firstly, the updateMany() method takes the argument to filter the documents where the field attendance is present.

Then, the second argument is called with the $set operator that specifies the changes to make to the selected documents. In this case, it sets the age field to 25 and the attendance field to Absent.

# Updating multiple fields in multiple documents
db.student.updateMany(
{ attendance: ‘Present’ },
{ $set: { age: 25, attendance: ‘Absent’ } }
)

The output shows that only one document is matched with the given filter object and only that document is updated.

We can also view the updated document with the specified age and attendance values.

3. Update Documents that Match a Specific Condition

We can also update multiple documents by giving a specific condition with a single command in MongoDB. In the following example, we have given a filter object that specifies which documents to update.

In this case, only documents where the value of the attendance field is Absent will be updated. After that, the update object is specified in the second argument that specifies the changes to make to the selected documents. The $mul operator is used to multiply the value of the age field by 2 for each selected document.

#Updating documents having conditions
db.student.updateMany(
{ attendance: ‘Absent’ },
{ $mul: { age: 2 } }
)

The output shows the documents are updated after matching the documents.

As you can see, the age field for documents where attendance is Absent has multiplied by 2.

4. By using the $in Operator

More like the above example, we can update the field by incrementing their values in multiple fields using the $in operator in the updateMany() method.

Consider the following query of the updateMany() method where the document is filtered first by specifying the condition that only documents where the value of the marks field is greater than 79 will be updated. Then, we used the $in operator which increments the value of the marks field by 1 for each selected document.

#Usage of $in operator
db.student.updateMany(
{ marks: { $gt: 79 } },
{ $inc: { marks: 1 } }
)

The output yielded acknowledged that documents that satisfy the condition are updated.

The find query shows that the marks field for documents where marks are greater than 79 has been incremented by 1.

5. Conclusion

In this article, we have covered examples of how to use the MongoDB updateMany() method to update multiple documents in different ways. By using this method, we can save time and reduce complexity when working with large datasets.

More details about this topic can be found here.

 Multiple documents can be updated simultaneously using the MongoDB updateMany() method. When we need to update a lot of documents in a collection, this technique comes in handy. In this article, we will discuss a few examples of updating multiple documents in MongoDB with a single command. Let’s consider we have the collection student which  Read More MongoDB 

Leave a Reply

Your email address will not be published. Required fields are marked *