Reputation: 24754
I try rename one field in all documents of a collection, with
db.coll.update({},{ $rename: {'originField':'newField'} });
but only one document is changed, why ?
Upvotes: 25
Views: 40673
Reputation: 43884
All updates in MongoDB are, by default, singular. You must add a third option to your command to make:
db.coll.update({},{ $rename: {'originField':'newField'} }, {multi:true});
If you are using 3.2 and above you can use updateMany()
:
db.coll.updateMany({}, {$rename: {'originField': "newField"}})
Upvotes: 50
Reputation: 21
Since MongoDB 3.2, you can use this shorter syntax:
db.coll.updateMany({}, {$rename: {'originField': "newField"}})
Upvotes: 2
Reputation: 385
db.collectionname.update( { "field" : "oldvalue" }, { $set:{ "field" : "newvalue" } }, { multi : true } );
Upvotes: 4