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?
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.
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.
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"
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.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With