Today I was faced with a situation where in I need to rename a field in all the MongoDB documents. The best way to do this is using $rename operator shown below.
db.post.update ( {}, { $rename : { "creationDate" : "creationdate" }},false,true )
Here true corresponds to updating all the all the documents i.e. multi is true.
From MongoDB documentation. Here’s the MongoDB shell syntax for update():
db.collection.update( criteria, objNew, upsert, multi )
Arguments:
- criteria – query which selects the record to update;
- objNew – updated object or $ operators (e.g., $inc) which manipulate the object
- upsert – if this should be an “upsert” operation; that is, if the record(s) do not exist, insert one. Upsert only inserts a single document.
- multi – indicates if all documents matching criteria should be updated rather than just one. Can be useful with the $ operators below.
better in mongdb 3.03
http://stackoverflow.com/questions/9254351/how-can-i-rename-a-field-for-all-documents-in-mongodb