MongoDB: How to update multiple documents with a single command?

302,928

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 })
Share:
302,928

Related videos on Youtube

Luke Dennis
Author by

Luke Dennis

Updated on September 06, 2021

Comments

  • Luke Dennis
    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
    TechplexEngineer over 11 years
    I'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
    zach almost 11 years
    what if I want set set different values to different document? is there a alegant way of doing this?
  • Anthony
    Anthony over 7 years
    TypeError: db.getCollection(...).find(...).update is not a function : ?
  • Prakash P
    Prakash P about 7 years
    didn'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
    Mahesh K over 6 years
    How to do it for oldvalue+"some string"
  • Nico
    Nico almost 3 years
    @Tyler explained, that the syntax he proposes works with the Mongo-Hacker he built - not out of the box.