Summary: in this tutorial, you’ll learn how to use the MongoDB updateMay()
method to update all the documents that match a condition.
Introduction to MongoDB updateMany() method
The updateMany()
method allows you to update all documents that satisfy a condition.
The following shows the syntax of the updateMany()
method:
db.collection.updateMany(filter, update, options)
Code language: CSS (css)
In this syntax:
- The
filter
is a document that specifies the condition to select the document for update. If you pass an empty document ({})
into the method, it’ll update all the documents the collection. - The
update
is a document that specifies the updates to apply. - The
options
argument provides some options for updates that won’t be covered in this tutorial.
The updateMany()
method returns a document that contains multiple fields. The following are the notable ones:
- The
matchedCount
stores the number of matched documents. - The
modifiedCount
stores the number of modified documents.
To form the update argument, you typically use the $set
operator.
$set operator
The $set
operator replaces the value of a field with a specified value. It has the following syntax:
{ $set: { <field1>: <value1>, <field2>: <value2>, ...}}
Code language: HTML, XML (xml)
If the field
doesn’t exist, the $set
operator will add the new field with the specified value
to the document. If you specify the field
with the dot notation e.g., embededDoc.field
and the field
does not exist, the $set
operator will create the embedded document (embedded
).
MongoDB updateMany() method examples
We’ll use the following products
collection:
db.products.insertMany([
{ "_id" : 1, "name" : "xPhone", "price" : 799, "releaseDate": ISODate("2011-05-14"), "spec" : { "ram" : 4, "screen" : 6.5, "cpu" : 2.66 },"color":["white","black"],"storage":[64,128,256]},
{ "_id" : 2, "name" : "xTablet", "price" : 899, "releaseDate": ISODate("2011-09-01") , "spec" : { "ram" : 16, "screen" : 9.5, "cpu" : 3.66 },"color":["white","black","purple"],"storage":[128,256,512]},
{ "_id" : 3, "name" : "SmartTablet", "price" : 899, "releaseDate": ISODate("2015-01-14"), "spec" : { "ram" : 12, "screen" : 9.7, "cpu" : 3.66 },"color":["blue"],"storage":[16,64,128]},
{ "_id" : 4, "name" : "SmartPad", "price" : 699, "releaseDate": ISODate("2020-05-14"),"spec" : { "ram" : 8, "screen" : 9.7, "cpu" : 1.66 },"color":["white","orange","gold","gray"],"storage":[128,256,1024]},
{ "_id" : 5, "name" : "SmartPhone", "price" : 599,"releaseDate": ISODate("2022-09-14"), "spec" : { "ram" : 4, "screen" : 5.7, "cpu" : 1.66 },"color":["white","orange","gold","gray"],"storage":[128,256]}
])
Code language: JavaScript (javascript)
1) Using the MongoDB updateMany() method to update multiple documents
The following example uses the updateMany()
method to update the documents where the value of the price field is 899
:
db.products.updateMany(
{ price: 899},
{ $set: { price: 895 }}
)
Code language: PHP (php)
In this query:
The { price : 899 }
is the filter
argument that specified the documents to update.
The { $set: { price: 895} }
specifies the update to apply, which uses the $set
operator to set the value of the price
field to 895
.
The query returns the following result:
{
acknowledged: true,
insertedId: null,
matchedCount: 2,
modifiedCount: 2,
upsertedCount: 0
}
Code language: CSS (css)
In this result document, the matchedCount
stores the number of matching documents (2) and the modifiedCount
stores the number of the updated documents (2).
To check the update, you can use the find()
method to select the documents where the value of the price
field is 895
as follows:
db.products.find({
price: 895
}, {
name: 1,
price: 1
})
Code language: CSS (css)
The query returned the following documents:
[
{ _id: 2, name: 'xTablet', price: 895 },
{ _id: 3, name: 'SmartTablet', price: 895 }
]
Code language: JavaScript (javascript)
The price
field values have been updated successfully.
2) Using the updateMany() method to update embedded documents
The following query uses the find()
method to select the documents where the value in the price
field is greater than 700:
db.products.find({
price: { $gt: 700}
}, {
name: 1,
price: 1,
spec: 1
})
Code language: CSS (css)
The query returned the following documents:
[
{
_id: 1,
name: 'xPhone',
price: 799,
spec: { ram: 4, screen: 6.5, cpu: 2.66 }
},
{
_id: 2,
name: 'xTablet',
price: 895,
spec: { ram: 16, screen: 9.5, cpu: 3.66 }
},
{
_id: 3,
name: 'SmartTablet',
price: 895,
spec: { ram: 12, screen: 9.7, cpu: 3.66 }
}
]
Code language: JavaScript (javascript)
The following example uses the updateMany()
method to update the values of the ram
, screen
, and cpu
fields in the spec
embedded documents of these documents:
db.products.updateMany({
price: { $gt: 700}
}, {
$set: {
"spec.ram": 32,
"spec.screen": 9.8,
"spec.cpu": 5.66
}
})
Code language: PHP (php)
The query returned the following document indicating that the three documents have been updated successfully:
{
acknowledged: true,
insertedId: null,
matchedCount: 3,
modifiedCount: 3,
upsertedCount: 0
}
Code language: CSS (css)
3) Using the MongoDB updateMany() method to update array elements
The following example uses the updateMany()
method to update the first and second elements of the storage
array of the documents where the _id is 1, 2 and 3:
db.products.updateMany({
_id: {
$in: [1, 2, 3]
}
}, {
$set: {
"storage.0": 16,
"storage.1": 32
}
})
Code language: PHP (php)
Output:
{
acknowledged: true,
insertedId: null,
matchedCount: 3,
modifiedCount: 3,
upsertedCount: 0
}
Code language: CSS (css)
If you query the documents whose _id
is 1, 2, and 3 from the products
collection, you’ll see that the first and second elements of the storage
array have been updated:
db.products.find(
{ _id: { $in: [1,2,3]}},
{ name: 1, storage: 1}
)
Code language: CSS (css)
Output:
[
{ _id: 1, name: 'xPhone', storage: [ 16, 32, 256 ] },
{ _id: 2, name: 'xTablet', storage: [ 16, 32, 512 ] },
{ _id: 3, name: 'SmartTablet', storage: [ 16, 32, 128 ] }
]
Code language: JavaScript (javascript)
Summary
- Use the
updateMany()
method to update all the documents within a collection that satisfy a condition. - Use the
$set
operator to replace the value of a field with a specified value.