MongoDB: How to update multiple documents with a single command?
Solution 1
Multi update was added recently, so is only available in the development releases (1.1.3). From the shell you do a multi update by passing true
as the fourth argument to update()
, where the the third argument is the upsert argument:
db.test.update({foo: "bar"}, {$set: {test: "success!"}}, false, true);
For versions of mongodb 2.2+ you need to set option multi true to update multiple documents at once.
db.test.update({foo: "bar"}, {$set: {test: "success!"}}, {multi: true})
For versions of mongodb 3.2+ you can also use new method updateMany()
to update multiple documents at once, without the need of separate multi
option.
db.test.updateMany({foo: "bar"}, {$set: {test: "success!"}})
Solution 2
Starting in v3.3 You can use updateMany
db.collection.updateMany(
<filter>,
<update>,
{
upsert: <boolean>,
writeConcern: <document>,
collation: <document>,
arrayFilters: [ <filterdocument1>, ... ]
}
)
In v2.2, the update function takes the following form:
db.collection.update(
<query>,
<update>,
{ upsert: <boolean>, multi: <boolean> }
)
https://docs.mongodb.com/manual/reference/method/db.collection.update/
Solution 3
For Mongo version > 2.2, add a field multi and set it to true
db.Collection.update({query},
{$set: {field1: "f1", field2: "f2"}},
{multi: true })
Solution 4
I've created a way to do this with a better interface.
-
db.collection.find({ ... }).update({ ... })
-- multi update -
db.collection.find({ ... }).replace({ ... })
-- single replacement -
db.collection.find({ ... }).upsert({ ... })
-- single upsert -
db.collection.find({ ... }).remove()
-- multi remove
You can also apply limit, skip, sort to the updates and removes by chaining them in beforehand.
If you are interested, check out Mongo-Hacker
Solution 5
To Update Entire Collection,
db.getCollection('collection_name').update({},
{$set: {"field1" : "value1", "field2" : "value2", "field3" : "value3"}},
{multi: true })
Related videos on Youtube
![Luke Dennis](https://i.stack.imgur.com/W5S5C.jpg?s=256&g=1)
Luke Dennis
Updated on September 06, 2021Comments
-
Luke Dennis almost 3 years
I was surprised to find that the following example code only updates a single document:
> db.test.save({"_id":1, "foo":"bar"}); > db.test.save({"_id":2, "foo":"bar"}); > db.test.update({"foo":"bar"}, {"$set":{"test":"success!"}}); > db.test.find({"test":"success!"}).count(); 1
I know I can loop through and keep updating until they're all changed, but that seems terribly inefficient. Is there a better way?
-
TechplexEngineer over 11 yearsI'm not sure about when this change happened but, Mongodb v2.2.2 does this a bit differently. db.collection.update( <query>, <update>, <options> ) where options is a set of key value pairs. See documentation: docs.mongodb.org/manual/applications/update
-
zach almost 11 yearswhat if I want set set different values to different document? is there a alegant way of doing this?
-
Anthony over 7 yearsTypeError: db.getCollection(...).find(...).update is not a function : ?
-
Prakash P about 7 yearsdidn't worked
db.userActivity.find({ 'appId' : 1234, 'status' : 1}).update({ $set: { 'status': 1 } }); 2017-06-05T17:47:10.038+0530 E QUERY [thread1] TypeError: db.userActivity.find(...).update is not a function :
-
Mahesh K over 6 yearsHow to do it for oldvalue+"some string"
-
Nico almost 3 years@Tyler explained, that the syntax he proposes works with the Mongo-Hacker he built - not out of the box.