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?
All latest versions of mongodb updateMany() is working fine
db.getCollection('workers').updateMany({},{$set: {"assignedVehicleId" : "45680"}});
To Update Entire Collection,
db.getCollection('collection_name').update({},
{$set: {"field1" : "value1", "field2" : "value2", "field3" : "value3"}},
{multi: true })
The following command can update multiple records of a collection
db.collection.update({},
{$set:{"field" : "value"}},
{ multi: true, upsert: false}
)
Thanks for sharing this, I used with 2.6.7 and following query just worked,
for all docs:
db.screen.update({stat:"PRO"} , {$set : {stat:"pro"}}, {multi:true})
for single doc:
db.screen.update({stat:"PRO"} , {$set : {stat:"pro"}}, {multi:false})
In the MongoDB Client, type:
db.Collection.updateMany({}, $set: {field1: 'field1', field2: 'field2'})
New in version 3.2
Params::
{}: select all records updated
Keyword argument multi
not taken
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 })
You can use.`
Model.update({
'type': "newuser"
}, {
$set: {
email: "[email protected]",
phoneNumber:"0123456789"
}
}, {
multi: true
},
function(err, result) {
console.log(result);
console.log(err);
}) `
MongoDB will find only one matching document which matches the query criteria when you are issuing an update command, whichever document matches first happens to be get updated, even if there are more documents which matches the criteria will get ignored.
so to overcome this we can specify "MULTI" option in your update statement, meaning update all those documnets which matches the query criteria. scan for all the documnets in collection finding those which matches the criteria and update :
db.test.update({"foo":"bar"},{"$set":{"test":"success!"}}, {multi:true} )
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/
I've created a way to do this with a better interface.
db.collection.find({ ... }).update({ ... })
-- multi updatedb.collection.find({ ... }).replace({ ... })
-- single replacementdb.collection.find({ ... }).upsert({ ... })
-- single upsertdb.collection.find({ ... }).remove()
-- multi removeYou can also apply limit, skip, sort to the updates and removes by chaining them in beforehand.
If you are interested, check out Mongo-Hacker
I had the same problem , and i found the solution , and it works like a charm
just set the flag multi to true like this :
db.Collection.update(
{_id_receiver: id_receiver},
{$set: {is_showed: true}},
{multi: true} /* --> multiple update */
, function (err, updated) {...});
i hope that helps :)
Source: Stackoverflow.com