How can I rename a collection in MongoDB? How can I rename a collection in MongoDB? mongodb mongodb

How can I rename a collection in MongoDB?


Close. Use db.originalCollectionName.renameCollection('newCollectionName')

See http://www.mongodb.org/display/DOCS/renameCollection+Command


For those who cannot rename, because the name causes an issue like: SyntaxError: Unexpected token ILLEGAL, it is because the name is illegal.

You can work around this by calling with brackets notation: db["oldCollectionILLEGALName"].renameCollection("someBetterName")


Assume that the database name is "mytestdb" and collection name is "orders". collection name change to orders2015 The simplest way is,

> use mytestdb> db.orders.renameCollection( "orders2015" )

Note : db.collection.renameCollection() is not supported on sharded collections.