JuanPablo
JuanPablo

Reputation: 24754

db.collection.update( ) all documents

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

Answers (3)

Sammaye
Sammaye

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

Josh
Josh

Reputation: 21

Since MongoDB 3.2, you can use this shorter syntax:

db.coll.updateMany({}, {$rename: {'originField': "newField"}})

Upvotes: 2

abdulH
abdulH

Reputation: 385

db.collectionname.update( { "field" : "oldvalue" }, { $set:{ "field" : "newvalue" } }, { multi : true } );

Upvotes: 4

Related Questions