Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I rename a collection in MongoDB?

Is there a dead easy way to rename a collection in mongo? Something like:

db.originalCollectionName.rename('newCollectionName'); 

And if not, what is the best way to go about effectively renaming one?

like image 699
Aaron Silverman Avatar asked Jan 04 '12 19:01

Aaron Silverman


People also ask

How do I rename a MongoDB collection in Atlas?

The renameCollection command renames a collection to the specified new name in the storage configuration. You can run this command only against the admin database, which is the Atlas user authentication database.

How do you rename a collection in MongoDB using Python?

The PyMongo function rename() is used to rename a collection. The rename operation fails if the new name is not an instance of basestring or it is an invalid collection name. Parameters : new_name : The new name of the collection.

How are collections named in MongoDB?

The general conventions are: Lowercase names: this avoids case sensitivity issues, as MongoDB collection names are case sensitive. Plural: more obvious to label a collection of something as the plural, e.g. "files" rather than "file"

How do I rename a Sharded collection?

There is no way to rename a sharded collection. You can copy all the docs to a new collection. Or create multiple collections based on a weekly/period date, and use as the current one. Then have your application always use the current one by name and change the name at each period break.


2 Answers

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

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

like image 153
nav Avatar answered Sep 21 '22 21:09

nav


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")

like image 33
Ev0oD Avatar answered Sep 24 '22 21:09

Ev0oD