In MongoDB the db.collection.updateMany()
method updates all documents that match the specified filter for a collection.
Example
Suppose we have a collection called pets
that contains the following documents:
{ "_id" : 1, "name" : "Wag", "type" : "Dog" } { "_id" : 2, "name" : "Bark", "type" : "Dog" } { "_id" : 3, "name" : "Meow", "type" : "Cat" }
We can see that two documents have Dog
as their type
.
We can update both documents at once like this:
db.pets.updateMany(
{ type: "Dog" },
{ $set: { type: "Cow" } }
)
Result:
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }
This shows us that two documents matched and two were updated.
We can check the collection:
db.pets.find()
Result:
{ "_id" : 1, "name" : "Wag", "type" : "Cow" }
{ "_id" : 2, "name" : "Bark", "type" : "Cow" }
{ "_id" : 3, "name" : "Meow", "type" : "Cat" }
Upsert
The db.collection.updateMany()
method accepts an upsert
argument that enables you to perform an upsert operation.
When upsert: true
, any documents that match the filter criteria are updated, but if there’s no match, a new document is inserted.
Let’s start with the original documents again:
{ "_id" : 1, "name" : "Wag", "type" : "Dog" } { "_id" : 2, "name" : "Bark", "type" : "Dog" } { "_id" : 3, "name" : "Meow", "type" : "Cat" }
Example:
db.pets.updateMany(
{ name: "Bubbles" },
{ $set: { type: "Fish" } },
{ upsert: true }
)
Result:
{ "acknowledged" : true, "matchedCount" : 0, "modifiedCount" : 0, "upsertedId" : ObjectId("5fe27e1dd991410169410244") }
In this case, there were no matches, so a document was upserted.
Let’s check the collection.
db.pets.find()
Result:
{ "_id" : 1, "name" : "Wag", "type" : "Dog" } { "_id" : 2, "name" : "Bark", "type" : "Dog" } { "_id" : 3, "name" : "Meow", "type" : "Cat" } { "_id" : ObjectId("5fe27e1dd991410169410244"), "name" : "Bubbles", "type" : "Fish" }
Embedded Documents
You can also use db.collection.updateMany()
to update embedded documents.
Suppose we insert the following documents:
db.pets.insertMany([
{
"_id" : 1,
"name" : "Wag",
"type" : "Dog",
"specs" : {
"height" : 400,
"weight" : 15,
"color" : "white"
}
},
{
"_id" : 2,
"name" : "Bark",
"type" : "Dog",
"specs" : {
"height" : 200,
"weight" : 12,
"color" : "white"
}
}
])
We can use the following code to update the embedded document.
db.pets.updateMany({
type: "Dog"
}, {
$set: {
"specs.color": "brown",
"specs.gooddog": false
}
})
Result:
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }
So we can see that both documents were updated.
Let’s check the document.
db.pets.find({
type: "Dog"
}).pretty()
Result:
{ "_id" : 1, "name" : "Wag", "type" : "Dog", "specs" : { "height" : 400, "weight" : 15, "color" : "brown", "gooddog" : false } } { "_id" : 2, "name" : "Bark", "type" : "Dog", "specs" : { "height" : 200, "weight" : 12, "color" : "brown", "gooddog" : false } }
We can see that the embedded documents were updated as specified.
Arrays
Let’s use db.collection.updateMany()
to update an array.
Suppose we have a collection called players
with the following documents:
{ "_id" : 1, "scores" : [ 1, 5, 3 ] } { "_id" : 2, "scores" : [ 8, 17, 18 ] } { "_id" : 3, "scores" : [ 15, 11, 8 ] }
Let’s update two array elements in all documents.
db.players.updateMany({},
{
$set: {
"scores.0": 20,
"scores.1": 26
}
})
Result:
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
We can see that all three documents were updated. This is because I left the filter criteria blank (I used {}
for the filter criteria).
And let’s look at the document.
db.players.find()
Result:
{ "_id" : 1, "scores" : [ 20, 26, 3 ] } { "_id" : 2, "scores" : [ 20, 26, 18 ] } { "_id" : 3, "scores" : [ 20, 26, 8 ] }
The arrayFilters Parameter
You can also use the arrayFilters
parameter and the positional $
operator to determine which array elements to update.
For example, looking at our previous document:
{ "_id" : 1, "scores" : [ 20, 26, 3 ] } { "_id" : 2, "scores" : [ 20, 26, 18 ] } { "_id" : 3, "scores" : [ 20, 26, 8 ] }
We could run the following query to update only those array elements that have a value higher than a certain amount (in this case 15).
db.players.updateMany(
{ scores: { $gte: 15 } },
{ $set: { "scores.$[e]" : 15 } },
{ arrayFilters: [ { "e": { $gte: 15 } } ] }
)
Result:
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
As expected, all documents match the criteria and are therefore updated. However, not all array elements were updated.
Here’s what the documents look like now.
db.players.find()
Result:
{ "_id" : 1, "scores" : [ 15, 15, 3 ] } { "_id" : 2, "scores" : [ 15, 15, 15 ] } { "_id" : 3, "scores" : [ 15, 15, 8 ] }
The only array elements that were updated were those with a value over 15.
More Information
The db.collection.updateMany()
method also accepts other parameters, such as writeConcern
, collation
, and hint
.
See the MongoDB documentation for db.collections.updateMany()
for more information.