How can I rename a collection in MongoDB?
Solution 1
Close. Use db.originalCollectionName.renameCollection('newCollectionName')
See http://www.mongodb.org/display/DOCS/renameCollection+Command
Solution 2
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")
Solution 3
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.
Solution 4
In case you are using Node.js MongoDB driver:
mongoClient.db(dbName).collection('oldName').rename("newName");
https://mongodb.github.io/node-mongodb-native/3.5/api/Collection.html#rename
my case was using mongoose:
await mongoose.connection.collection("oldName").rename("newName");
Solution 5
Rename a collection in cmd:
cd C:\Program Files\MongoDB\Server\4.2\bin
mongo
use yourdb
db.yourcollection.renameCollection("someBetterName")
This example is made for MongoDB 4.2
![Aaron Silverman](https://i.stack.imgur.com/0mTzm.jpg?s=256&g=1)
Aaron Silverman
SVP Engineering at Benefix.us. Co-creator of Doodle Or Die. Formerly Head of Engineering at stock media startup Storyblock, Senior Software Engineer at energy efficiency start-up Opower, and Lead Software Engineer at business analytic startup Applied Predictive Technologies. Washington, DC transplant to beautiful Ouray, Colorado via Philadelphia. Father to three children. Owner of two rescued dogs. Rock and Ice Climber. Instrument-rated private pilot.
Updated on March 14, 2021Comments
-
Aaron Silverman over 3 years
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?
-
nav over 12 yearshint: try using tab complete to find things you think might be there ;)
-
AlexP over 9 years+1 I also found that this will error if the collection that you wish to rename to (the 'target collection name') already exists. IF you are happy for said collection to be dropped; then you can pass 'true' as the second argument, e.g.
db.originalCollName.renameCollection('alreadyExistingCollName', true)
-
user2810081 over 7 yearsafter the rename, will the indexes and everything be kept?
-
Liswin about 3 years
db.getCollection("oldCollectionILLEGALName").renameCollection("someBetterName")
should also work. I had renamed my collection to_old_collection_name
and this saved me. -
Azan Momin about 3 yearsThanks buddy, this solved my issue. I am currently learning Mongo and in a rush I ended up naming my collection as 'employee-detail' and was stuck with the error. This was a good learning.
-
Timo about 2 yearsAnswer from 2015 still valid as of today, plus one.
-
Timo about 2 yearsHow can I make the
old
one variable, such asdb[old].rename..
? -
Timo about 2 yearsAlways remember,
mongo
is notjs
is notnodejs
. So look at the difference, therefore plus one.